使用 List<type> 而不是 ArrayList<Hashmap<String,String>> 用于列表视图

Use a List<type> instead of ArrayList<Hashmap<String,String>> for list view

我有一个对象:

private ArrayList<HashMap<String, String>> customerDataList;

我使用从 Web 服务调用中检索到的数据填充其中。下面是在循环中迭代检索到的 json 数据:

HashMap<String, String> customer = new HashMap<>();
//snip
customer.put("CustomerName", customerName);
//snip
customerDataList.add(customer);
//rinse and repeat

然后我在 activity 的列表视图中显示它:

ListAdapter adapter = new SimpleAdapter(Activity.this, customerDataList,
                R.layout.list_item, new String[] {"CustomerName"}, new int[] { R.id.customerName });
setListAdapter(adapter);

但是我想知道是否有一种方法可以使用列表来代替。

我创建了我的客户 class,像这样创建了一个列表:

private List<Customer> customerList;

但是,在设置 ListAdapter 时,我不确定要放什么。我用 customerList 替换了 customerDataList 但我收到错误。

编辑:错误是

SimpleAdapter() in SimpleAdapter cannot be applied to:
Expected data: java.util.Map<java.lang.String.?>>
Actual arguments: customerList <Customer>

编辑:我的 class 结构

private class Customer
{
    private int customerID;
    private String customerName;
    private String customerLocation;
    private String runningTime;
    private double distance;
}

您可以使用

List<Customer>

但您需要使用自定义适配器而不是简单适配器。

需要设置包含customer对象的数据,简单适配器只能接受name value pare map。所以你需要扩展 adapter/Basadpter class 的要求。

如果您想要使用自定义对象列表,您需要使用另一个适配器,例如ArrayAdapter:

List<Customer> customerList = null;
ArrayAdapter<Customer> customerAdapter = new ArrayAdapter<Customer>(this,
        R.layout.list_item, customerList);

现在,由于 ArrayAdapter 期望字符串来自您的自定义对象,因此请覆盖 class 中的 toString 方法:

class Customer {
    String customerName;

    @Override
    public String toString() {
        return customerName;
    }
}

如果您的客户 class 有多个字段需要在您的适配器中使用,您将需要一个自定义适配器,如下所示:

// Turns out that when the layout is not a TextView, you need to provide the id
// of the TextView the adapter can bind to
ArrayAdapter<Customer> customerAdapter = new ArrayAdapter<Customer>(this,
        R.layout.list_item, R.id.customer_name, customerList) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        TextView name = view.findViewById(R.id.customer_name);
        TextView surname = view.findViewById(R.id.customer_surname);

        name.setText(getItem(position).customerName);
        surname.setText(getItem(position).customerSurname);

        return view;
    }
};

您可以google找到自定义适配器的一些性能调整。

最简单的方法是将 Customer 对象的值分配给列表行布局中的视图,即创建一个扩展 ArrayAdapter class:

public class CustomAdapter extends ArrayAdapter<Customer> {

private final Context context;
  private final Customer[] values;

  public MySimpleArrayAdapter(Context context, Customer[] values) {
    super(context, R.layout.rowlayout, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null {
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = inflater.inflate(R.layout.listviewrowlayout, parent, false);
    }

    TextView customerName= (TextView) convertView.findViewById(R.id.customerName);
    TextView customerAge= (TextView) convertView.findViewById(R.id.customerAge);

    customerName.setText(values[position].getName());
    customerAge.setText(values[position].getAge());

    return convertView;
  }
}