標籤:android android開發 simpleadapter java 介面
SimpleAdapter:將List集合的多個對象封裝成清單項目。
我們可以通過SimpleAdapter來實現一些複雜的列表,請看以下執行個體:
simpleadapter_list布局檔案:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_simpleadapter" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView></LinearLayout></span>
simpleadapter布局檔案:用於顯示清單項目的組件
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/ll_top" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/iv_image" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="fitXY" android:src="@drawable/a5i" android:layout_gravity="center"/> <LinearLayout android:id="@+id/ll_right" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="4dp"> <TextView android:id="@+id/tv_title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:text="AS"/> <TextView android:id="@+id/tv_des" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="13sp" android:text="AS" /> </LinearLayout></LinearLayout></RelativeLayout></span>
SimpleAdapterTest主檔案:
<span style="font-size:18px;">public class SimpleAdapterTest extends Activity {int[] images = new int[] { R.drawable.a5i, R.drawable.a5j, R.drawable.a5k };String[] titles = new String[] { "電話", "QQ", "連絡人" };String[] des = new String[] { "當前無電話記錄", "當前無QQ聊天記錄", "最近無連絡人" };private ListView lv_simple;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.simpleadapter_list);initView();setData();}private void initView() {lv_simple = (ListView) findViewById(R.id.lv_simpleadapter);lv_simple.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(SimpleAdapterTest.this, titles[position],Toast.LENGTH_SHORT).show();}});}private void setData() {// 建立資料來源List<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < titles.length; i++) {// 每個清單項目的內容HashMap<String, Object> listItem = new HashMap<String, Object>();listItem.put("image", images[i]);listItem.put("title", titles[i]);listItem.put("desc", des[i]);listItems.add(listItem);}/* * SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) * 參數說明: * context:整個應用的上下文。 * data:是List<? extends Map<String, ?>>的集合對象,其中每個Map<String, ?>代表每列的清單項目內容。 * resource:介面布局檔案的ID。 * from:String[]類型的參數,指定了Map<String, ?>中的每個key對應的value來產生清單項目。 * to:int[]類型的參數,顯示每個清單項目顯示的組件。 */SimpleAdapter simpleAdapter = new SimpleAdapter(SimpleAdapterTest.this,listItems, R.layout.simpleadapter, new String[] { "image","title", "desc" }, new int[] { R.id.iv_image,R.id.tv_title, R.id.tv_des });//綁定適配器lv_simple.setAdapter(simpleAdapter);}}</span>
轉載請註明出處:http://blog.csdn.net/hai_qing_xu_kong/article/details/42361041 情緒控_
一起學android之SimpleAdapter使用(13)