views:

1893

answers:

2

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.

+3  A: 

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;
}
}
DroidIn.net
Thanks. That helped a lot. Also this API sample I just found was very helpful: http://developer.android.com/guide/samples/ApiDemos/src/com/example/android/apis/view/List14.html
znq
of course you don't want to implement getView() like that, it's just an example (use convertView).
Matthias
+1  A: 

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.

gabe