I want to put one arrayList inside another arrayList, but I have some problems with that.
Basicly this is what I want:
ArrayList<ObjectThatContainArraylist> myList1;
ArrayList<ObjectThatContain3Strings> myList2;
I want myList2 few times as an objects inside myList1, so when I get(position) of mylist1, I will get in each get(position) a different arraylist with 3 different strings.
So that I have 2 listviews in my mainActivity, the first listview is supposed to show the indexes of myList1 and it does. When clicking on one of its items it send the position from onSetItemListener to the adapter of the second listview, in which the listview is supposed to show myList2 with 3 Strings in each item/index of the listview, but it only shows one index instead of all the indexes I added earlier.
Question
Is the method I used here correct? Is it the right and easiest way to get an arraylist of objects (with 3 Strings in it) inside an arraylist which needs to hold few of those arraylists with objects inside them (see image below)?
Edit:
This is what I am trying to do:
This is my adapter:
public class AdapterForSecondListView extends BaseAdapter {
ArrayList myList = new ArrayList();
Context context;
LayoutInflater inflater;
int positionClickedInParentArray;
@Override
public ArrayList<ObjectParcelable> getItem(int position) {
return (ArrayList<ObjectParcelable>) myList.get(position);
}
public AdapterForChildListView(Context context, ArrayList myList, int positionClickedInParentArray) {
this.context = context;
this.myList = myList;
inflater = LayoutInflater.from(context);
this.positionClickedInParentArray = positionClickedInParentArray;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate (R.layout.items_for _second_listview, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
final ArrayList<ObjectParcelable> currentListData = getItem(positionClickedInParentArray);
if (position<currentListData.size()) {
mViewHolder.firstTitle.setText(currentListData.get(position).getFirstTitle());
mViewHolder.secondTitle.setText(currentListData.get(position).getSecondTitle());
mViewHolder.description.setText(currentListData.get(position).getDescription());
}
return convertView;
}
private class MyViewHolder {
TextView firstTitle;
TextView secondTitle;
TextView description;
public MyViewHolder(View item) {
firstTitle = (TextView) item.findViewById(R.id.firstTitleOfChildArray);
secondTitle = (TextView) item.findViewById(R.id.secondTitleOfChildArray);
description = (TextView) item.findViewById(R.id.descriptionOfChildArray);
}
}
}
// this is the method that supposed to add the object to the position,
// it gets both the object and the position from mainActivity
// in the logs i printed the size of the second position, and when running
// in the emulator i tried to add only to the first position,
// and it add to the both of them
public void getDataInListForSecondListView(Context context, ObjectParcelable objectParcelable, int position) {
Log.d("Tab5", "Before : ParentList.get(1).size() = " + String.valueOf(parentList.get(1).size()));
parentList.get(position).add(objectParcelable);
Log.d("Tab5", "After : ParentList.get(1).size() = " + String.valueOf(parentList.get(1).size()));
}
