ListView和Map的值为List

时间:2014-09-20 11:43:04

标签: android android-listview

有没有办法从

填充Android ListView
Map<Integer, List<Object>> 

我想做这样的事情:

-Key1

-Value11

-Value12

-Value13

-Key2

-Value21

-Value22

-Value23

我试过了:

将适配器添加到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();
    }
}

我认为主要问题是map.size()返回此映射中键 - 值映射的数量。

我的错误:

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)
**

2 个答案:

答案 0 :(得分:1)

使用ExpandableListView找到解决方案。

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

这是一个例子,我很容易为Map重新制作。

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

答案 1 :(得分:0)

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