【Android】兩種適配器的使用

來源:互聯網
上載者:User

標籤:

ArrayAdapter適配器:
(1)用於顯示基本的文字內容
(2)基本使用過程:建立適配器---建立或載入資料來源---適配器載入資料來源---視圖載入適配器
ArrayAdapter(上下文,當前ListView載入的每一個清單項目所對應的布局檔案,資料來源)
(ArrayAdapter)資料配接器的實現過程:
1.建立適配器

arr_adapter=new ArrayAdapter<String>(context,布局檔案,資料來源)

arr_adapter=new ArrayAdapter<String>(上下文(this),當前ListView載入的每一個清單項目所對應的布局檔案(android.R.layout.simple_list_item_1),資料來源(String[]arr_data={"1","2","3","4"}));
2.添加資料來源到適配器(上一步已包含)
3.視圖(ListView)載入適配器
listView.setAdapter(arr_adapter);

 

使用SimpleAdapter

private ListView listView;

private SimpleAdapter simple_adapter;

//聲明適配器

private List<Map<String,Object>> dataList;
dataList=new ArrayList<Map<String,Object>>();
//1、建立一個資料配接器—SimpleAdapter();

//2、適配器載入資料來源
simple_adapter = new SimpleAdapter(this,getData()*擷取資料來源*,R.layout.布局檔案名稱,newString[]{"索引值名1","索引值名2"},new int[]{R.id.控制項名1,R.id.控制項名2});

 

//3、視圖(ListView)載入適配器
listView.setAdapter(simple_adapter);

 

// 產生資料來源
private List<Map<String, Object>> getData() {
for(int i=0;i<20;i++){
Map<String,Object> map=new HashMap<String,Object>();
map.put("索引值名",R.drawable.ic_launcher);
map.put("索引值名","測試文本"+i);
dataList.add(map);
}
return dataList;
}

注意:map.put("pic",R.drawable.ic_launcher);map.put("text","測試文本"+i);這裡的pic和text與new int[] { R.id.pic,R.id.text}無關(只是同名罷了),是關聯於new String[] {"pic", "text"}中的名字。

 

 

【Android】兩種適配器的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.