【发布时间】: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() 会损害可读性
-
斯特凡,谢谢你的回答。