【问题标题】:Populating a single listview with multiple custom array lists [closed]使用多个自定义数组列表填充单个列表视图 [关闭]
【发布时间】:2015-10-12 11:56:50
【问题描述】:

我有 3 个数组列表,它们从各种模型类中获取数据。我想在一个带有单独标题的列表视图中显示这 3 个。我怎样才能做到这一点?谁能帮我?

【问题讨论】:

  • 在android中引用listview

标签: android


【解决方案1】:

您可以使用ExpandableListView,其中您的数据源是ArrayMap<String, ArrayList<Object>>

这是一个不完整的示例,说明如何管理不同模型的视图。

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        switch (groupPosition) {
            case 1 :
                //TODO: if the layout is different for each group you should check if the convertedView is the one you should be using for this group. 
                //TODO: if it isn't you should inflate, if it is you should reuse
                if (convertView == null || !convertView.getTag() instanceof KittyViewHolder.class) {
                    convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_kitties, null);
                    holder = new KittyViewHolder();
                    holder.name = (TextView) view.findViewById(R.id.kittyname);
                    convertView.setTag(holder);
                }
                else {
                    holder = (KittyViewHolder) view.getTag();
                }

                //You can savely cast since you know your first group contains Kitties (=^・^=)
                Kitty aKitty = (Kitty) getChild(groupPosition, childPosition);
                holder.name.setText(aKitty.getName());
                break;
            case 2:
                //TODO: if the layout is different for each group you should check if the convertedView is the one you should be using for this group. 
                //TODO: if it isn't you should inflate, if it is you should reuse
                if (convertView == null || !convertView.getTag() instanceof SharkHolder.class) {
                    convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_sharks, null);
                    holder = new SharkHolder();
                    holder.thooth = (TextView) view.findViewById(R.id.thoothcount);
                    holder.picture = (ImageView) view.findViewById(R.id.sharkimage);
                    convertView.setTag(holder);
                }
                else {
                    holder = (SharkHolder) view.getTag();
                }

                //You can savely cast since you know your second group contains Sharks
                Shark aShark = (Shark) getChild(groupPosition, childPosition);
                holder.thooth.setText(""+aShark.getToothCount());
                holder.picture.setImageResource(aShark.getBabySharkPicture());
                break;
            case 3:
                //Same thing as above with the correct model type
                break;
        }

        return convertView;
    }

【讨论】:

  • 我正在使用可扩展的列表视图。我需要为同一个列表视图设置不同的适配器。这可能吗?因为所有 3 个模型类都是不同的。
  • 我不明白为什么不这样做。如果您的模型类共享一个公共父级或接口,您的 ArrayList 将是 ArrayList,否则它将是 ArrayList。您需要检查 getGroupViewgetChildView 您正在处理的对象。
  • 如果我将相同的列表视图设置为两个适配器,它只显示一组适配器。
  • 我编辑了我的答案,希望它是你要找的。你的问题被搁置了,因为太宽泛了。您应该编辑您的问题并添加一些您到目前为止所做的代码和/或说您正在使用可扩展列表视图。
  • 我现在面临一个小问题。我有 3 个标题,在这些标题下显示了三个数组列表。单击其中一个列表时,我将列表大小设为零,但数据显示正确。单击工作正常,列表大小也第一次正确显示。当我从下一个屏幕返回并再次单击列表时,会出现此问题。你能帮我解决这个问题吗?
【解决方案2】:

你应该学习如何使用部分。

您可以在 SO 上阅读此 answer。或者直接关注这些tutorial(也出现在原始答案中)。

在您的情况下,您应该有一个具有三个不同数据数组的适配器。我们称他们为array1array2array3

你应该有以下getCount方法:

public int getCount() {
     return array1.length + array2.length + array3.length + 3;//3 for 3 header
}

那么你应该有 4 个项目视图类型(每个数组一个,标题一个)

public int getItemViewTypeCount() {
     return 4
}

然后你应该告诉你的适配器每个位置的视图类型

public int getItemViewType(int position) {
     if (position == 0) {
           return VIEW_TYPE_HEADER;
     } else if (position < 1 + array1.length) {
           return VIEW_TYPE_1;
     } else if (position == 1 + array1.length) {
            return VIEW_TYPE_HADER;
     } else if (position < 2 + array1.length + array2.length) {
           return VIEW_TYPE_2;
     } else if (position == 2 + array1.length + array2.length) {
            return VIEW_TYPE_HADER;
     } else if (position < 3 + array1.length + array2.length + array3.length) {
           return VIEW_TYPE_3;
     } else {
            return 0;//this should never happened
     }
}

现在你可以实现getView

public View getView(int position, View convertView, ViewGroup parent) {
      switch (getItemViewType(position)) {
            case VIEW_TYPE_HEADER:
                    getHeaderView(positin, convertView, parent);
                    break;
            case VIEW_TYPE_1:
                    getViewType1(positin - 1, convertView, parent);
                    break;
            case VIEW_TYPE_2:
                    getViewType1(positin - 2 - array1.length, convertView, parent);
                    break;
            case VIEW_TYPE_2:
                    getViewType1(positin - 3 - array1.length - array2.length, convertView, parent);
                    break;
            default:
                    return null;
       }
 }

然后您应该像往常一样实现应该为对象arrayX[position] 构建视图的getViewTypeX(int position, View convertView, ViewGoup parent)

【讨论】:

  • 这对于字符串数组来说看起来很简单。但是当涉及到自定义模型时,发现很难实现。我可以为同一个列表视图设置三个不同的适配器,因为我有 3 个自定义数组列表吗?
  • 我编辑我的答案以使其更容易理解(我希望)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
相关资源
最近更新 更多