使用List <type>而不是ArrayList <hashmap <string,string>&gt;列表视图</hashmap <string,string> </type>

时间:2015-01-10 19:07:03

标签: java android arraylist

我有一个对象:

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

然后我在我的活动的列表视图中显示:

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

但是我想知道是否有办法可以使用List。

我创建了我的客户类,就像这样创建了一个List:

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>

编辑:我的班级结构

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

3 个答案:

答案 0 :(得分:1)

如果您想使用自定义对象列表,则需要使用其他适配器,例如ArrayAdapter

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

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

class Customer {
    String customerName;

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

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

// 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;
    }
};

您可以谷歌查找自定义适配器的一些性能调整。

答案 1 :(得分:0)

您可以使用

List<Customer>

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

需要设置包含客户对象的数据,简单适配器只能接受名称值pare map。所以你需要扩展适配器/ Basadpter类的要求。

答案 2 :(得分:0)

最简单的方法是将Customer对象中的值分配给列表行布局中的视图,以创建扩展ArrayAdapter类的自定义适配器:

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;
  }
}