重新編輯下問題說明:我想通過simpleadapter輸出arraylist的值,由于我是指定位置的,當這個ArrayList某個位置有值時,第一個位置就會出現與其相同的結果。ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();for (int j = 0; j < 3; j++)
list.add(null);HashMap<String, Object> map = new HashMap<String, Object>();map.put("name", "jim");map.put("year", 2009);
list.add(2, map);
ListView listview = (ListView)findViewById(R.id.listview);
listview.setAdapter(new SimpleAdapter(MainActivity.this, list, R.layout.item, new String[]{"name","year"}, new int[]{R.id.name,R.id.year}) {
});
1 回答

千巷貓影
TA貢獻1829條經驗 獲得超7個贊
第一,ArrayList不是Array,容量自動增加,不需要初始化。好吧事實上,Array也不需要初始化,因為新生成的Array所有值都是null或者primitive type的默認值,除非你用initializer。
第二,add不是賦值,如果不確定,RTFM
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
而賦值是這個,同理請記得RTFM。
第三,如果你需要的是保證初始的容量足夠大,用帶參數的構造函數
添加回答
舉報
0/150
提交
取消