SimpleAdapter理解
生活随笔
收集整理的這篇文章主要介紹了
SimpleAdapter理解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
? ? ?? SimpleAdapter ? ?? ?? ?? ? 1,作用是ArrayList和 ListView的橋梁。 這個ArrayList里邊的每一項都是一個Map<String,?>類型。
? ?? ?ArrayList當中的每一項 Map對象都和ListView里邊的每一項進行數據綁定一一對應。
? ?? ?? 2,SimpleAdapter的構造函數:
SimpleAdapter(Context??context, List<? extends Map<String, ?>>??data, int resource, String[]??from, int[] to)
? ?? ?參數:
? ?? ?1,context:上下文。
? ?? ?2,data:基于Map的list。Data里邊的每一項都和 ListView里邊的每一項對應。Data里邊的每一項都是一個Map類型,這個Map類里邊包含了ListView每一行需要的數據。
? ?? ?3,resource :就是一個布局layout,可引用系統提供的,也可以自定義。
? ?? ?4,from:這是個名字數組,每個名字是為了在 ArrayList數組的每一個item索引Map<String,Object>的Object用的。
? ?? ?5,to:里面是一個TextView數組。這些 TextView是以id的形式來表示的。例如:Android.R.id.text1,這個text1在layout當中是可以索引的。
? ?? ? 3,通過一個類子來理解下:
? ?? ?listitem.xml文件:
java代碼:
<?xml version=”1.0″ encoding=”utf-8″?>
? ? <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
? ? android:orientation=”horizontal” android:layout_width=”fill_parent”
? ? android:layout_height=”wrap_content”>
? ? <TextView android:id=”@+id/mview1″ android:layout_width=”100px”
? ? android:layout_height=”wrap_content” />
? ? <TextView android:id=”@+id/mview2″
? ? android:layout_width=”wrap_content”
? ? android:layout_height=”wrap_content” />
? ? </LinearLayout>
? ?? ? 下面是activity文件的部分代碼
java代碼:
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
? ? Map<String,Object> item;
? ? item = new HashMap<String,Object>();
? ? item.put(“姓名”,”張三”);
? ? item.put(“性別”,”男”);
? ? data.add(item);
? ? item = new HashMap<String,Object>();
? ? item.put(“姓名”,”李四”);
? ? item.put(“性別”,”女”);
? ? data.add(item);
? ? // 上面是構造數據部分。
? ? ListView listview= new ListView(this);
? ? //構造listview對象。
? ? SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.listitem,new String[]{“姓名”,”性別”},new int[]{R.id.TextView01,R.id.TextView02});
? ? /*構造一個適配器。
? ? *? ? 1,第三個參數是說明用的是自定義的布局R.layout.listtiem。
? ? *??2,第四和第五個參數一起理解:
? ? *? ?? ?? ? 把我們添加數據時姓名那一列對應到R.id.TextView01這個TextView中,把性別對應到R.id.TextView02這個 TextView中。
? ? *? ?? ?? ? 如果把from改為new String[]{“姓名”,”姓名”},測試下就會明白。
? ? */
? ? listview.setAdapter(adapter);
? ? setContentView(listview);
轉載于:https://my.oschina.net/MrGuan/blog/55227
總結
以上是生活随笔為你收集整理的SimpleAdapter理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: chvg改变vg中LV的数量
- 下一篇: 【转】Android使用嵌入式关系型SQ