標籤:android style blog http color 使用
ListView之SimpleAdapter
SimpleAdapter的建構函式是:
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
參數context:上下文,比如this。關聯SimpleAdapter啟動並執行視圖上下文
參數data:Map列表,列表要顯示的資料,這部分需要自己實現,如例子中的getData(),類型要與上面的一致,每條項目要與from中指定條目一致
參數resource:ListView單項布局檔案的Id,這個布局就是你自訂的布局了,你想顯示什麼樣子的布局都在這個布局中。這個布局中必須包括了to中定義的控制項id
參數 from:一個被添加到Map上關聯每一個項目列名稱的列表,數組裡面是列名稱
參數 to:是一個int數組,數組裡面的id是自訂布局中各個控制項的id,需要與上面的from對應。
下面是相關的程式碼片段,雖然不是專為做講解用的例子,但SimpleAdapter的用法是完整的。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recall); SysApplication.getInstance().addActivity(this); listView = (ListView) this.findViewById(R.id.listview); listView.setAdapter(getAdapter()); } protected SimpleAdapter getAdapter() { List<User> users = getUsers(); List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); for (User u : users) { HashMap<String, String> temp = new HashMap<String, String>(); temp.put("name", u.getUsers_Name()); data.add(temp); } SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), data, R.layout.recall_item, new String[] { "name" }, new int[] { R.id.name }); Log.i("info", "填充資料完成"); return adapter; }
一下是對應的:(紅色字型為ListView中顯示)