Sarmad Aijaz Sarmad Aijaz - 2 months ago 17
Java Question

I want two listviews to scroll at the same time

I have an activity in which I have two listviews.

One is custom listview with imageview and textview insdie and another one will be a button after I find solution of scrolling.

Basically left is a list for food and its picture and right side is list for buying it.

Now I have problem in scrolling them at same time.

public class list_activity extends AppCompatActivity {

String[] itemname ={
"Safari",
"Camera",
"Global",
"FireFox",
"UC Browser",
"Android Folder",
"VLC Player",
"Cold War",
"Whatsapp",
"Google",
"Java"
};
String[] buy={"one","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two"};
Context context = this;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
ListAdapter adapter = new com.example.azaabudeen.restoapp.CustomListView(this,itemname);
ListView listView= (ListView)findViewById(R.id.listview);
ListView secondlistview= (ListView)findViewById(R.id.foodlistBuyButton);
ListAdapter secondA= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,buy);
secondlistview.setAdapter(secondA);
listView.setAdapter(adapter);

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.view);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String currentItem= String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(list_activity.this,currentItem,Toast.LENGTH_SHORT).show();

/// final TextView text = (TextView)findViewById(R.id.text);
/// final Button myButton=(Button)findViewById(R.id.dialogButtonOK);
/// text.setText("Image of the list");
/// myButton.setOnClickListener(new View.OnClickListener() {
/// @Override
/// public void onClick(View v) {
//// dialog.dismiss();
// }
/// });
dialog.show();
}
});
}
}


Update:

I did it by individually adding onClickListners to Customadapter
here is the code

class CustomListView extends ArrayAdapter {
public CustomListView(Context context, String[] resource) {
super(context,R.layout.custom_view , resource);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater MyInflater = LayoutInflater.from(getContext());
View CustomView = MyInflater.inflate(R.layout.custom_view, parent, false);
String SingleItem= (String) getItem(position);
TextView text =(TextView)CustomView.findViewById(R.id.Itemname);
ImageView Image= (ImageView)CustomView.findViewById(R.id.icon);

text.setText(SingleItem);
Image.setImageResource(R.drawable.myimage);
final Button Buybutton= (Button)CustomView.findViewById(R.id.BuyButton);
Buybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Bought", Toast.LENGTH_SHORT).show();
}
});


Image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"Image",Toast.LENGTH_SHORT).show();

}
});
return CustomView;
}
}

Answer

You should just use one ListView.

You can set onClickListener inside your custom Adapter.

An example is here: ListView with clickListener

Set onClickListener for your imageView and for your button.

Comments