0

I have a TreeMap with key value pairs, The key is a String which denotes a certain type. Based on this there can be four more properties of the type which I add as a list. Now for each entry of <|key|,list |string> I want to populate one single row in the list view. What type of Adapter should I make. Also I have to keep in view that I have to override the getView() method because I want to display different pictures depending on the key value. Any Hints or tutorials?

User3
  • 2,465
  • 8
  • 41
  • 84

2 Answers2

0

You can use simple base adapter..In that adapter, in get view method, u can use another list adapter to inflate that list

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

You can easily get away with using BaseAdapter. The trick here is to get a key-value pair from the map at the specified index. This isn't that difficult - and with your map being sorted you'll get the same order every time. You'll have something like this:

public class MapAdapter<K, V> extends BaseAdapter {
    Context context;
    Map<K, V> data;

    public MapAdapter(Context _context, Map<K, V> _data) {
        context = _context;
        data = _data;
    }

    public int getCount() { return data.size(); }

    public Object getItem(int position) {
        K key = map.keySet().toArray()[position];
        V value = map.get(key);
        return AbstractMap.SimpleEntry(key, value);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        AbstractMap.SimpleEntry<K, V> entry = (AbstractMap.SimpleEntry<K, V>)getItem(position);
        K key = entry.getKey();
        V value = entry.getValue();

        MyRowView rowView = (MyRowView)convertView;
        if(rowView == null) {
            rowView = ...    //create your view by inflating or otherwise
        }

        //Now you have the view and details of your key and value - populate the row
        ...

        return rowView;
    }
}
Aleks G
  • 56,435
  • 29
  • 168
  • 265