Android中的Adapter簡(jiǎn)單介紹
更新時(shí)間:2013年06月02日 15:19:38 作者:
Android中的Adapter簡(jiǎn)單介紹,需要的朋友可以參考一下
Android中的Adapter在自定義顯示列表時(shí)非常有用,比如SimpleAdapter,它的構(gòu)造函數(shù)是:
public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)
它的各參數(shù)的意思:
1.context,上下文,SimpleAdapter關(guān)聯(lián)的視圖,一般而言就是當(dāng)前的Activity,this
2.data,泛型的List,如ArrayList,Map或者HashMap
3.resource,資源文件,一個(gè)R.layout,就是要顯示的布局
4.from ,一個(gè)數(shù)組,Map中的鍵值對(duì)。
5.to,layout的xml文件中命名id形成的唯一的int型標(biāo)識(shí)符
比如:
在一個(gè)ListActivity中定義一個(gè)List:
List> people= new ArrayList>();
Map m=new HashMap();
m.put("name","tom");
m.put("age","20");
people.add(m);
...
SimpleAdapter adapter = new SimpleAdapter(this,
(List>) feets, R.layout.main,
new String[] { "name","age" }, new int[] {R.id.name,R.id.age });
setListAdapter(adapter);
其中:
R.id.name,R.id.age 是在一個(gè)XML布局文件中定義的兩個(gè)用于顯示name和age的TextView。布局文件中要有一個(gè)ListView。或者在程序中定義也可以。
另外,注意在ListActivity中不需要設(shè)置setContentView,系統(tǒng)被自動(dòng)加載。
public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)
它的各參數(shù)的意思:
1.context,上下文,SimpleAdapter關(guān)聯(lián)的視圖,一般而言就是當(dāng)前的Activity,this
2.data,泛型的List,如ArrayList,Map或者HashMap
3.resource,資源文件,一個(gè)R.layout,就是要顯示的布局
4.from ,一個(gè)數(shù)組,Map中的鍵值對(duì)。
5.to,layout的xml文件中命名id形成的唯一的int型標(biāo)識(shí)符
比如:
在一個(gè)ListActivity中定義一個(gè)List:
List> people= new ArrayList>();
Map m=new HashMap();
m.put("name","tom");
m.put("age","20");
people.add(m);
...
SimpleAdapter adapter = new SimpleAdapter(this,
(List>) feets, R.layout.main,
new String[] { "name","age" }, new int[] {R.id.name,R.id.age });
setListAdapter(adapter);
其中:
R.id.name,R.id.age 是在一個(gè)XML布局文件中定義的兩個(gè)用于顯示name和age的TextView。布局文件中要有一個(gè)ListView。或者在程序中定義也可以。
另外,注意在ListActivity中不需要設(shè)置setContentView,系統(tǒng)被自動(dòng)加載。
您可能感興趣的文章:
- android開(kāi)發(fā)中ListView與Adapter使用要點(diǎn)介紹
- 詳解Android App中ViewPager使用PagerAdapter的方法
- Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
- Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
- Android listview與adapter詳解及實(shí)例代碼
- Android開(kāi)發(fā)中ListView自定義adapter的封裝
- Android Adapter的幾個(gè)常用方法
- Android中GridView和ArrayAdapter用法實(shí)例分析
- Kotlin編寫Android適配器Adapter
- Android ListView適配器(Adapter)優(yōu)化方法詳解
- Android開(kāi)發(fā)中總結(jié)的Adapter工具類【附完整源碼下載】
相關(guān)文章
Flutter使用?input?chip?標(biāo)簽組件示例詳解
這篇文章主要為大家介紹了Flutter使用?input?chip?標(biāo)簽組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android實(shí)現(xiàn)注冊(cè)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)注冊(cè)頁(yè)面之監(jiān)聽(tīng)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android帶清除功能的輸入框控件EditTextWithDel
這篇文章主要為大家詳細(xì)介紹了Android帶清除功能的輸入框控件EditTextWithDel,感興趣的小伙伴們可以參考一下2016-09-09Android開(kāi)發(fā)使用ProgressBar實(shí)現(xiàn)進(jìn)度條功能示例
這篇文章主要介紹了Android開(kāi)發(fā)使用ProgressBar實(shí)現(xiàn)進(jìn)度條功能,結(jié)合實(shí)例形式分析了Android進(jìn)度條ProgressBar的具體樣式、布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03Android?SharedPreferences性能瓶頸解析
這篇文章主要為大家介紹了Android?SharedPreferences性能瓶頸解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android頂部工具欄和底部工具欄的簡(jiǎn)單實(shí)現(xiàn)代碼
Android頂部工具欄和底部工具欄的簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理
今天小編就為大家分享一篇關(guān)于CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12