【问题标题】:How to Display Two ArrayList values in Android ListView?如何在 Android ListView 中显示两个 ArrayList 值?
【发布时间】:2012-07-05 06:10:12
【问题描述】:

我是新的 Android 开发。在我的应用程序中,我有两个 ArrayList。

一个 ArrayList 包含 HashTbale。 另一个包含HashTable

现在我如何设置适配器以及如何在第二个 ArrayList 的 Listview 图像和 ListView Row 的 ArrayList 的文本中显示。

我无法得到确切的解决方案。

请任何人帮助解决这个问题。

提前致谢

【问题讨论】:

  • 如果可能,请提供两个 HashMap 值的更多详细信息。如果可能的话,我可以建议您将它们合并到一个哈希图中。易于处理。
  • @PareshMayani 感谢您的回复。我的确切要求是在 LIstView 中,我需要显示图像和文本。但是我不能在一个 ArrayList 中同时维护这两个,这就是我使用两个 arraylist 的原因。但我无法使用两个 ArrayLists 为 listview 设置适配器
  • 只需使用存储图像路径和文本的 HashMap 数组列表,并将单个 ArrayList 设置为适配器。您也可以根据您的要求制作自定义对象 ArrayList。只是谷歌你可以找到很多例子..

标签: android listview android-emulator arraylist android-2.2-froyo


【解决方案1】:

如果您想在每一行中显示图像和文本,并且您使用 2 个数组列表来显示相同​​的内容,那么这里是解决方案。试试这个

List<Integer> imageList = getImageList();  //list of drawable ids
List<String> values = getValues();

class CustomAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imageList.size();;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // inflate the layout which contains imageview and textview which are aligned horizontally. 
        //Assuming you inflated layout and got imageView and textview from that layout
        textView.setText(values.get(position));

        imageView.setImageResource(imageList.get(position));
        return convertView;
    }

}

//如果直接复制粘贴,代码将不起作用。使用逻辑并相应地改变

我的建议是你使用 HashMap,而不是使用 2 arraylist。图像路径作为键,文本作为值。

【讨论】:

  • @PareshMayani 我同意。但他想使用 2 个数组列表。所以只有我给出了这个解决方案
  • 如果两个数组列表的大小不同怎么办。还是Text和Image不相关?
【解决方案2】:

第一个建议:(未提出)

你知道如何为 ListView 定义自定义适配器吗?如果是,那么您可以轻松地将两个数组列表传递给您的自定义适配器。在getView() 方法中,您可以在 ListView 中显示您想要的任何细节

第二个建议:(提出的解决方案)

如果你制作了一个CustomObject的ArrayList,那么就不需要在Adapter中传递两个ArrayList。

【讨论】:

  • 如果他做了一个CustomObject的ArrayList那么就不需要在Adapter中传递两个ArrayList了。
  • @user370305 这就是我试图在上面的 cmets 中传达的想法。
  • 如果两个数组列表的大小不同,则第一个建议不起作用。或者 Text 和 Image 互不相关。
猜你喜欢
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 2016-04-05
  • 2014-09-08
  • 2018-07-12
  • 2015-12-27
相关资源
最近更新 更多