Android中的Adapter簡單介紹
更新時間:2013年06月02日 15:19:38 作者:
Android中的Adapter簡單介紹,需要的朋友可以參考一下
Android中的Adapter在自定義顯示列表時非常有用,比如SimpleAdapter,它的構造函數是:
public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)
它的各參數的意思:
1.context,上下文,SimpleAdapter關聯(lián)的視圖,一般而言就是當前的Activity,this
2.data,泛型的List,如ArrayList,Map或者HashMap
3.resource,資源文件,一個R.layout,就是要顯示的布局
4.from ,一個數組,Map中的鍵值對。
5.to,layout的xml文件中命名id形成的唯一的int型標識符
比如:
在一個ListActivity中定義一個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 是在一個XML布局文件中定義的兩個用于顯示name和age的TextView。布局文件中要有一個ListView?;蛘咴诔绦蛑卸x也可以。
另外,注意在ListActivity中不需要設置setContentView,系統(tǒng)被自動加載。
public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)
它的各參數的意思:
1.context,上下文,SimpleAdapter關聯(lián)的視圖,一般而言就是當前的Activity,this
2.data,泛型的List,如ArrayList,Map或者HashMap
3.resource,資源文件,一個R.layout,就是要顯示的布局
4.from ,一個數組,Map中的鍵值對。
5.to,layout的xml文件中命名id形成的唯一的int型標識符
比如:
在一個ListActivity中定義一個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 是在一個XML布局文件中定義的兩個用于顯示name和age的TextView。布局文件中要有一個ListView?;蛘咴诔绦蛑卸x也可以。
另外,注意在ListActivity中不需要設置setContentView,系統(tǒng)被自動加載。
您可能感興趣的文章:
- android開發(fā)中ListView與Adapter使用要點介紹
- 詳解Android App中ViewPager使用PagerAdapter的方法
- Android中 自定義數據綁定適配器BaseAdapter的方法
- Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實現(xiàn))
- Android listview與adapter詳解及實例代碼
- Android開發(fā)中ListView自定義adapter的封裝
- Android Adapter的幾個常用方法
- Android中GridView和ArrayAdapter用法實例分析
- Kotlin編寫Android適配器Adapter
- Android ListView適配器(Adapter)優(yōu)化方法詳解
- Android開發(fā)中總結的Adapter工具類【附完整源碼下載】
相關文章
AndroidStudio3.6的卸載安裝,Gradle持續(xù)下載/Gradle Build失敗等問題
這篇文章主要介紹了AndroidStudio3.6的卸載安裝,Gradle持續(xù)下載/Gradle Build失敗等問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
android多媒體音樂(MediaPlayer)播放器制作代碼
這篇文章主要為大家詳細介紹了android多媒體音樂(MediaPlayer)播放器的制作相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android巧用DecorView實現(xiàn)對話框功能
本篇文章主要介紹了Android巧用DecorView實現(xiàn)對話框功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Android實現(xiàn)單頁面浮層可拖動view的示例代碼
本篇文章主要介紹了Android實現(xiàn)單頁面浮層可拖動view的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
詳解Android中使用Notification實現(xiàn)進度通知欄(示例三)
這篇文章主要介紹了詳解Android中使用Notification實現(xiàn)進度通知欄(示例三),具有一定的參考價值,有興趣的可以了解一下。2016-12-12
Android Studio EditText點擊圖標清除文本內容的實例解析
這篇文章主要介紹了Android Studio EditText點擊圖標清除文本內容的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11

