简体   繁体   English

使用清单 <type> 而不是ArrayList <Hashmap<String,String> &gt;用于列表视图

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

I have an object: 我有一个对象:

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

Which I populate with data retrieved from a web service call. 我用从Web服务调用中检索到的数据填充。 The below is inside a loop iterating over the retrieved json data: 以下是循环访问检索到的json数据的循环:

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

Then I display this in a list view in my activity with: 然后,我在活动列表视图中显示以下内容:

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

However I am wondering if there is a way I can use a List instead. 但是我想知道是否有一种方法可以使用列表。

I created my customer class, created a List of it like so: 我创建了客户类,并创建了它的列表,如下所示:

private List<Customer> customerList;

However when it comes to setting the ListAdapter I am unsure of what to put. 但是,当涉及到设置ListAdapter时,我不确定要放置什么。 I replaced customerDataList with customerList but I receive errors. 我用customerList替换了customerDataList,但是收到错误。

Edit: The error is 编辑:错误是

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

Edit: my class structure 编辑:我的班级结构

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

If you want a use a list of custom objects you need to use another adapter eg ArrayAdapter : 如果要使用自定义对象的列表,则需要使用另一个适配器,例如ArrayAdapter

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

Now since ArrayAdapter expects strings to come out of your custom objects, override a toString method in your class: 现在,由于ArrayAdapter希望字符串来自您的自定义对象,因此请在您的类中重写toString方法:

class Customer {
    String customerName;

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

If your customer class has multiple fields that will need to be used in your adapter, you will need a custom adapter, something like this: 如果您的客户类别有多个字段需要在适配器中使用,则需要一个自定义适配器,如下所示:

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

You can google to find some performance adjustments for custom adapters. 您可以在Google上找到自定义适配器的一些性能调整。

You can use 您可以使用

List<Customer>

but you need to go for a custom adapter instead of Simple adapter. 但您需要使用自定义适配器而不是简单适配器。

The data containing the customer object need to be set, the simple adapter can only accept the name value pare map. 需要设置包含客户对象的数据,简单适配器只能接受名称值pare映射。 So you need to extend the adapter/Basadpter class the requirement. 因此,您需要扩展适配器/ Basadpter类的要求。

The easiest way is to assign the values from Customer object to views in your list row layout is to create a custom Adapter extending the ArrayAdapter class: 最简单的方法是将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;
  }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 stream 过滤器获取 hashmap 列表? ArrayList <hashmap<string, object> &gt; </hashmap<string,> - How use stream filter for list of hashmap? ArrayList<HashMap<String, Object>> 如何分配ArrayList哈希图 <String,Double[]> 列出 - How to Assign ArrayList hashmap<String,Double[]> to List 比较HashMap <String, Double> 和清单 <ArrayList<String> &gt;在Java中 - Compare HashMap<String, Double> and List<ArrayList<String>> in Java 将值添加到Arraylist中 <string,List<String> &gt;在hashmap android中 - add the value in Arraylist<string,List<String>> in hashmap android 如何在列表中存储HashMap <String,ArrayList <String >>? - How can I store HashMap<String, ArrayList<String>> inside a list? List <HashMap <String,Object >>的Thrift类型建模 - Thrift type modelling for List<HashMap<String,Object>> 如何在HashMap中对数组列表进行排序 <Integer, ArrayList<String> &gt; - how to sort the array list in HashMap<Integer, ArrayList<String>> 允许JComboBox接受列表 <String> 或HashMap而不是String [] - Allow JComboBox to accept List<String> or HashMap instead of String[] 如果我使用HashMap <String, ArrayList<String> &gt;在Java中 - If I use HashMap<String, ArrayList<String>> in Java 请求回复未来<string, string, list<product> &gt; 未映射,而是映射到 ArrayList<linkedhashmap></linkedhashmap></string,> - RequestReplyFuture<String, String, List<Product>> not mapped, Instead it mapped to ArrayList<LinkedHashMap>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM