经过多个列表<串GT;到ArrayAdapter [英] Passing Multiple Lists<String> into ArrayAdapter

查看:151
本文介绍了经过多个列表<串GT;到ArrayAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开始的活动与此:

adapter = new ItemAdapter(Items.this, items, totals);
        setListAdapter(adapter);

现在这里是ItemAdapter:

Now here is ItemAdapter:

public class ItemAdapter extends ArrayAdapter<String> {

private final List<String> items;
private final List<String> totals;
private final Context context;

public ItemAdapter(Context context, List<String> items,
        List<String> totals) {
    super(context, R.layout.item_row_layout, items);
    this.context = context;
    this.items = items;
    this.totals = totals;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater
            .inflate(R.layout.item_row_layout, parent, false);
    TextView t1 = (TextView) rowView.findViewById(R.id.itemName);
    TextView t2 = (TextView) rowView.findViewById(R.id.itemTotal);

    String s1 = items.get(position);
    t1.setText(s1);

    String s2 = totals.get(position);
    t2.setText(s2);


    return rowView;
}

}

现在我知道问题出在构造函数,因为直到只允许我一个列表,而不是两个传递到它。是否有另一种方式来做到这一点?

Now I know the problem is with the constructor because till only allow me to pass one List, not two into it. Is there another way to do this?

推荐答案

我建议使用的 SimpleAdapter 。这里是如何将列表转换成一个列表&LT;地图&LT; ... >>(但我建议只是建立一个列表&LT;地图&LT; ... >>当你构建单独的列表):

I suggests using a SimpleAdapter. Here is how to convert your Lists into one List< Map<...>> (but I recommend just building one List< Map<...>> when you are building the separate lists):

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map;
int count = items.size();
for(int i = 0; i < count; i++) {
    map = new HashMap<String, String>();
    map.put("name", items.get(i));
    map.put("total", totals.get(i));
    list.add(map);
}

adapter = new SimpleAdapter(this, list, R.layout.item_row_layout, new String[] { "name", "total" }, new int[] { R.id.itemName, R.id.itemTotal });

现在你的名称将自动显示在自己的看法一排,而无需编写自定义适配器。

Now your name and total are automatically displayed in one row in their own Views without having to write a custom adapter.

这篇关于经过多个列表&LT;串GT;到ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆