将多个 Lists<String> 传递到 ArrayAdapter

The*_*ter 0 android constructor list android-arrayadapter

我从这个活动开始:

adapter = new ItemAdapter(Items.this, items, totals);
        setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

现在这里是 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;
}

}
Run Code Online (Sandbox Code Playgroud)

现在我知道问题出在构造函数上,因为直到只允许我传递一个 List,而不是两个。有没有另一种方法可以做到这一点?

Sam*_*Sam 5

我建议使用SimpleAdapter。以下是如何将您的列表转换为一个 List< Map<...>>(但我建议您在构建单独的列表时只构建一个 List< Map<...>>):

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 });
Run Code Online (Sandbox Code Playgroud)

现在您的nametotal会自动显示在自己的视图中的一行中,而无需编写自定义适配器。