【问题标题】:arraylist inside arraylist in androidandroid中的arraylist里面的arraylist
【发布时间】:2017-12-15 11:09:10
【问题描述】:

我想将一个 arrayList 放在另一个 arrayList 中,但是我有一些问题。

基本上这就是我想要的:

ArrayList<ObjectThatContainArraylist> myList1;
ArrayList<ObjectThatContain3Strings> myList2;

我希望myList2 几次作为myList1 中的对象,所以当我得到mylist1(position) 时,我将在每个get(position) 中获得一个包含3 个不同字符串的不同数组列表。

所以我的mainActivity 中有2 个listviews,第一个listview 应该显示myList1 的索引,它确实如此。单击其中一个项目时,它将位置从onSetItemListener 发送到第二个listview 的适配器,其中listview 应该显示myList2,@987654337 的每个项目/索引中都有 3 个字符串@,但它只显示一个索引,而不是我之前添加的所有索引。

问题

我这里使用的方法正确吗?它是在一个数组列表中获取一个对象数组列表(其中包含 3 个字符串)的正确且最简单的方法吗?该数组列表中需要保存其中一些包含对象的数组列表(见下图)?

编辑:

这就是我想要做的:

这是我的适配器:

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()));
    }

【问题讨论】:

  • 请添加一些代码
  • 所以你想要一个多级列表视图?
  • 您的问题不清楚,但是.. 您可以使用 List> list =new ArrayList();
  • 考虑为这种复杂的数据结构使用单独的类。嵌套的 ArrayList() 会损害可读性
  • 斯特凡,谢谢你的回答。

标签: java android arraylist


【解决方案1】:

您可以创建List&lt;List&lt;YourModelClass&gt;&gt; list = new ArrayList();

【讨论】:

  • 这应该是一条评论。
  • 帕拉维,谢谢您的回答。我只是想知道如何将另一个对象添加到内部列表中,我的意思是:我如何调用外部列表的 position = 2 并将字符串对象添加到该列表中?
【解决方案2】:

找到解决办法

ArrayList<ArrayList<String>> mainArrayList = new ArrayList<ArrayList<String>>();
ArrayList<String> subArrayList = new ArrayList<String>();
mainArrayList.add(subArrayList );

【讨论】:

    【解决方案3】:

    @shon 检索或更新内部列表,然后您需要将内部列表分配给新列表,并对新分配的列表进行更改并根据外部列表位置更新到内部列表。

    【讨论】:

      【解决方案4】:

      要创建这样的列表,您可以这样做:

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

      如果您想向该父列表添加新的子列表,请这样做:

      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);
      

      要将字符串添加到特定位置的列表中,您可以这样做:

      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);
      }
      

      更新

      我假设您在 listview1 中有片段/活动 a。您单击 listview1 的一个项目并打开一个包含 listview2 的新片段/活动。现在您必须将数据传递给新的片段/活动。

      listview2 的适配器可能如下所示:

      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;
        }
      }
      

      这就是你创建适配器的方式:

      // 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);
      

      如果您为 listview2 创建新的片段/活动,则必须传递要在 listview2 中显示的对象列表。为此,您必须实现一个 onclicklistener。以this 为例。

      【讨论】:

      • 非常感谢,我认为这是我的解决方案,我只需要知道如何在列表视图的适配器中使用它,以便在它们的位置显示字符串。哦,顺便说一句,我不单独使用 3 个字符串,我在自定义对象中使用它们,因为每个 childList 都有几组 3 个字符串...
      • 如果我明白你想要什么,你必须这样做:parentlist.get(position).getStrings();(你得到一个特定位置的自定义对象并从自定义对象中获取字符串)。这就是您必须在适配器中使用它的方式。如果没有您的适配器代码,我无法为您提供更多帮助...
      • 我添加了我的适配器,我的适配器似乎正确但它不起作用...:/
      • 我使用了你的解决方案,我正在使用它来将我的对象添加到子列表 parentList.get(position).add(ObjectParcelable); (位置等于我想要添加对象的位置)但是这是将相同的对象添加到父列表的所有位置,而不是添加到我告诉它的特定位置,知道为什么这样做吗?顺便说一句,我尝试使用日志来跟踪尺寸,即使我没有要求它将对象添加到任何其他位置,它仍然会将相同的对象添加到所有位置..
      • 你在哪里调用add函数?
      猜你喜欢
      • 2018-08-17
      • 1970-01-01
      • 2013-05-15
      • 2019-03-20
      • 2020-04-04
      • 2017-01-24
      • 2017-08-20
      • 2012-01-12
      • 1970-01-01
      相关资源
      最近更新 更多