Shunt
Shunt

Reputation: 189

ListView and Map with values as List

Is there any way to populate android ListView from

Map<Integer, List<Object>> 

?

I would like to do something like this:

-Key1

-Value11

-Value12

-Value13

-Key2

-Value21

-Value22

-Value23

I've tried:

Adding adapter to listview:

ListAdapter adapterList = new StudiesAdatper(FullWeek.this, studiesMap);
((ListView) findViewById(R.id.weekListView)).setAdapter(adapterList);

StudiesAdapter:

private class StudiesAdatper extends BaseAdapter {

    private Map<Integer, List<Study>> map;
    private Context context;
    private int day = 0;

    public StudiesAdatper(Context context, Map<Integer, List<Study>> map) {
        this.context = context;
        this.map = map;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.day_row, null);
        }


        if (map.get(day) == null) {
            day++;
            return convertView;
        }

        Study study = map.get(day++).get(position);
        /* 
         * Working with study here
         */
        return convertView;

    }

    @Override
    public int getCount() {
        return map.size();
    }
}

I think the main problem is that map.size() returns the number of key-value mappings in this map.

My error:

FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
**

Upvotes: 0

Views: 101

Answers (2)

Shunt
Shunt

Reputation: 189

Found solution by using ExpandableListView.

http://developer.android.com/reference/android/widget/ExpandableListView.html

Here is example, which i easily re-made for Map.

http://www.mysamplecode.com/2012/10/android-expandablelistview-example.html

Upvotes: 1

NavinRaj Pandey
NavinRaj Pandey

Reputation: 1704

List<Study>ls=(List<Study>)map.get(day++);
if(ls.size()-1>=position){
   Study study = map.get(day++).get(position);
}

Upvotes: 0

Related Questions