Android: ListView with complex data model

时光总嘲笑我的痴心妄想 提交于 2019-11-28 04:35:58

问题


I'd like to map an Array of "complex" data to a ListView. In a very simplified form my data model would look like something like this:

class ListPlacesValues {

  String idObject;
  String name;
  String city;
  String country;
  ArrayList<String> classification;
  double distance_quantity;
  DistanceUnit distance_unit;
            [...more stuff ...]
}

I know that I can convert my complex data into a HashList and then just use a SimpleAdapter:

   SimpleAdapter mAdapter = new SimpleAdapter(
     this,
     hashList,
     R.layout.places_listitem,
     new String[] { "name", "city", "country"}, 
     new int[] { R.id.name, R.id.city, R.id.country}
   );  

However, I would rather use my data model directly, but I've no idea where and how to start, so that in the end I can do something like this:

ArrayList<ListPlacesValues> values = getData();  
MyAdapter mAdapter = new MyAdapter(
          this,
          values,
          R.layout.places_listitem,
          ListPlacesValues { values.name, values.city, values.country}, 
          new int[] { R.id.name, R.id.city, R.id.country}
);  

Solution: I found this Android API sample (List14), which was really helpful.


回答1:


You can extend ArrayAdapter. Here's code example for you. In this example - SearchItem is some custom POJO. Basically you need to override getView() method to build your row by inflating row layout and then populating values based on List of items and current position

class SearchItemsAdapter extends ArrayAdapter<SearchItem> {
Activity context;
List<SearchItem> items;
SearchHeader header;

@SuppressWarnings("unchecked")
public SearchItemsAdapter(final Activity context,
        final Map<SearchHeader, List<SearchItem>> result) {
    super(context, R.layout.item, (List) ((Object[]) result.values()
            .toArray())[0]);
    this.context = context;
    this.header = result.keySet().iterator().next();
    this.items = result.get(this.header);
}

@Override
public View getView(final int position, final View convertView,
        final ViewGroup parent) {
    final View view = this.context.getLayoutInflater().inflate(
            R.layout.item, null);
    final SearchItem item = this.items.get(position);
    ((TextView) view.findViewById(R.id.jt)).setText(item.jt);
    ((TextView) view.findViewById(R.id.dp)).setText(item.dp);
    ((TextView) view.findViewById(R.id.cn)).setText(item.cn);
    ((TextView) view.findViewById(R.id.loc)).setText(item.loc.name);
    final TextView body = ((TextView) view.findViewById(R.id.e));
    body.setText(item.e);
    body.setTag(item.src[0]);
    ((TextView) view.findViewById(R.id.src)).setText(item.src[1]);
    return view;
}
}



回答2:


There is one pitfall with the convertView in the sample you linked

if(convertView != null){ //reuse
   convertView.setAnimation(null);
   convertView.setAnyCustomFieldsIdontWantFilledWithData(null);
}

you want to set all animations or unused fields to null otherwise your items might have data in them or animations pending you dont want.



来源:https://stackoverflow.com/questions/1595429/android-listview-with-complex-data-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!