2014/05/30

Android 自訂ListView

難在於SimpleAdapter建構方法要帶入什麼參數
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

Context:使用當前context即可
List:傳入資料
Resource:自訂ListView layout ID
From:是設定哪些Key對應到哪些資料
To:自訂ListView layout裡面物件ID

Code:
package com.cy.mylistview;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

 private SimpleAdapter adapter;
 private ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
 private ListView listView;

 private final String[] TAG = { "name", "address", "age" };
 private final int[] ID = { R.id.textViewName, R.id.textViewAddress,
   R.id.textViewAge };
 private final String[] NAME = { "小明", "小華", "小妻", "小王" };
 private final String[] ADDRESS = { "台北", "台中", "台南", "高雄" };
 private final int[] AGE = { 20, 21, 22, 23 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  init();
 }

 private void init() {
  listView = (ListView) findViewById(R.id.listView);

  for (int index = 0; index < 4; index++) {
   HashMap<String, Object> item = new HashMap<String, Object>();
   item.put(TAG[0], NAME[index]);
   item.put(TAG[1], ADDRESS[index]);
   item.put(TAG[2], AGE[index]);
   arrayList.add(item);
  }

  adapter = new SimpleAdapter(getApplicationContext(), arrayList,
    R.layout.mylistview, TAG, ID);
  listView.setAdapter(adapter);
 }
}




activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

mylistview.xml:
<?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:background="#333"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textViewAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

成果:


參考資料:
http://blog.joomla.org.tw/android/178
http://developer.android.com/reference/android/widget/SimpleAdapter.html