2014-09-20 49 views
0

有什麼辦法來填充從ListView和地圖與值列表

Map<Integer, List<Object>> 

的Android的ListView?

我願做這樣的事情:

-Key1

-Value11

-Value12

-Value13

-Key2

-Value21

-Value22

-Value23

我已經試過:

添加適配器列表視圖:

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()返回鍵的數量在這張地圖中的值映射。

我的錯誤:通過使用ExpandableListView

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

你會得到什麼錯誤? – 2014-09-20 11:45:27

+0

IndexOutOfBoundsException。 – Shunt 2014-09-20 11:48:45

回答

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

是的,這固定了錯誤,但映射的主要思想不起作用...此代碼不顯示它的所有元素。我認爲做到這一點的唯一方法是將所有地圖元素合併到一個列表中......但是,多虧了你。 – Shunt 2014-09-21 05:31:44

+0

所以這個列表沒有按照它應該的方式填充? – 2014-09-21 07:29:20

+0

是的,它僅填充來自地圖第一個鍵的一些(不是全部)值。 – Shunt 2014-09-21 07:52:49