1

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:

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()));
    }
5
  • please add some code Commented Jul 11, 2017 at 11:48
  • So you want to have a multilevel listview? Commented Jul 11, 2017 at 11:52
  • your question not clear but .. You can use List<List<YourObject>> list =new ArrayList(); Commented Jul 11, 2017 at 11:53
  • Consider using separate class for this kind of complex data structure. Nested ArrayList() will hurt readability Commented Jul 11, 2017 at 12:03
  • i just wonder how can i add another object to the inside list, how can i call position = 2 of the outside list and add an object of Strings to that list? Commented Jul 11, 2017 at 12:07

4 Answers 4

4

Find the solution

ArrayList<ArrayList<String>> mainArrayList = new ArrayList<ArrayList<String>>();
ArrayList<String> subArrayList = new ArrayList<String>();
mainArrayList.add(subArrayList );
Sign up to request clarification or add additional context in comments.

Comments

0

You can create List<List<YourModelClass>> list = new ArrayList();

2 Comments

this should be a comment.
Pallavi, thank you for your answer. i'm just wondering how can i add another object to the inside list, i mean: how can i call position = 2 of the outside list and add to that list an object of Strings?
0

@shon to retrieve or update inside list then you need to assign inside list to new list and made changes to new assigned list and update to the inside list based on outside list position.

Comments

0

To create such a list you can do it like this:

ArrayList<ArrayList<String>> parentlist = new ArrayList<ArrayList<String>>();

If you want to add a new child list to that parent list do it like that:

ArrayList<String> childlist = new ArrayList<String>();
childlist.add("a");
childlist.add("b");
childlist.add("c");

// add the childlist to the parent list
parentlist.add(childlist);

To add strings to a list of a specific position you can do this:

ArrayList<ArrayList<String>> parentlist = new ArrayList<ArrayList<String>>();

// get the childlist on position 2
ArrayList<String> childlist = parentlist.get(2);

// if childlist is not null the list already exist and you can add your strings
// otherwise you have to create a new list and add it to your parent list
if(childlist != null) {
  childlist.add("a");
  childlist.add("b");
  childlist.add("c");
}
else {
  childlist = new ArrayList<String>();
  childlist.add("a");
  childlist.add("b");
  childlist.add("c");

  // add the new childlist to the parent list because it didn't exist
  parentlist.add(childlist);
}

UPDATE

I assume you have fragment/activity a with listview1. You click on an item of listview1 and open a new fragment/activity which includes listview2. Now you have to pass the data to that new fragment/activity.

The adapter of listview2 could look like this:

public class ObjectParcelableAdapter extends ArrayAdapter<ObjectParcelable> {
  public ObjectParcelableAdapter (Context context, ArrayList<ObjectParcelable> objects) {
    super(context, 0, objects);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    ObjectParcelable object = getItem(position);    

    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
      convertView =LayoutInflater.from(getContext()).inflate(R.items_for _second_listview, parent, false);
    }

    // set your textviews values here
    return convertView;
  }
}

And this is the way you create the adapter:

// Create the adapter to convert the array to views
UsersAdapter adapter = new ObjectParcelableAdapter(this, listofobjects);

// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.listview2);
listView.setAdapter(adapter);

And if you create the new fragment/activity for listview2 you have to pass the list of objects you want to show in listview2. For that you have to implement an onclicklistener. Check this for an example.

12 Comments

Thank you very much, i think this is my solution, i just need to know how to use it in the adapter of the list view, in order to show the Strings in their places. oh and btw, i don't use the 3 Strings saperate, i use them in a custom object because every childList has few sets of 3 Strings...
If i understand what you want you have to do it like this: parentlist.get(position).getStrings(); (you get the custom object of a specific position and get the strings from the custom object). That's the way you have to use it in your adapter. Without code of your adapter i can't help you more...
i added my adapter, my adapter seems correct but it doesn't work... :/
I used your solution, and I am using this to add my object to the child list parentList.get(position).add(ObjectParcelable); (position equels the location i want to add the object to) but this is adding the same object to all the positions of the parent list instead to just the specific position i told it to, any idea why this is doing that? btw i tried putting logs to track on the sizes, and even though I am not asking it to add the object to any other position it still add the same object to all the positions..
Where do you call the add function?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.