I have more then one ArrayList think that ArrayList1,ArrayList2,ArrayList3... In ArrayList1 have some images showing it in GridView android, when I click on images it should show ArrayList2 in that GridView only and ArrayList2 also having images. Same for ArrayList2 and ArrayList3. How to achieve this
Asked
Active
Viewed 115 times
1 Answers
1
Assuming that all your ArrayLists contain the same object type.
Create a new ArrayList, lets call it AdapterArrayList and use it as the main ArrayList for the Adapter. Now If you want to use ArrayList1, clear()
elements from AdapterArrayList and then addAll()
the ArrayList1. and of course notifyDataSetChanged()
for the Adapter.
Do the same when you want to use ArrayList2 and ArrayList3.
[Edit]
For example:
public class MyAdapter extends BaseAdapter {
List<Image> mImagesList;
public MyAdapter(List<Image> imagesList) {
mImagesList = imagesList;
...
}
}
And to use that adapter with the ArrayLists you have:
public class MyActivity extends Activity {
MyAdapter mAdapter;
List<Image> mainList = new ArrayList();
List<Image> arrayList1 = new ArrayList();
List<Image> arrayList2 = new ArrayList();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mAdapter = new MyAdapter(mainList);
myListView.setAdapter(mAdapter);
}
private void switchToList1() {
mainList.clear();
mainList.addAll(arrayList1);
mAdapter.notifyDataSetChanged();
}
private void switchToList2() {
mainList.clear();
mainList.addAll(arrayList2);
mAdapter.notifyDataSetChanged();
}
}

Abdallah Alaraby
- 2,222
- 2
- 18
- 30
-
@AbdallalhAlaraby Give me a example or code something so that I can refer – May 24 '16 at 11:04
-
@AbdallalhAlaraby how can I send id of selected grid element to the next grid? – May 25 '16 at 14:10
-
This is a totally different question that should have it's own thread. anyway, you can define an `OnClickListener` inside the Adapter's `getView()` method. check [this link](http://stackoverflow.com/questions/20191914/how-to-add-gridview-setonitemclicklistener) – Abdallah Alaraby May 26 '16 at 08:39