編輯:關於 Android 程式設計
ListView 是 Android 最常用來呈現資料列表的元件。本文從最簡單的字串清單出發,逐步帶到讀取聯絡人、以及搭配 SimpleAdapter 顯示圖文並包的進階用法,幫助你快速掌握 ListView 的各項操作。
先在版面配置檔中放上一個 ListView,這裡順便示範了分隔線、背景與快取顏色提示等屬性:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/icon"
android:cacheColorHint="#00000000"
android:background="@drawable/icon66" >
</ListView>
</LinearLayout>
接著在 Activity 中用 ArrayAdapter 把字串陣列塞進去:
public class ListView1Activity extends Activity {
String[] shujuStrings = { "wwww", "wwww", "erfff" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
shujuStrings);
listView1.setAdapter(adapter);
}
}
透過 ContentResolver 取得聯絡人 Cursor,再用 SimpleCursorAdapter 綁定到列表上,是 ListView 與系統資料結合的常見寫法:
public class ListViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = new ListView(this);
// 取得手機聯絡人
@SuppressWarnings("deprecation")
Cursor cursor = getContentResolver()
.query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
ListAdapter listAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_expandable_list_item_1,
cursor, new String[] { People.NAME },
new int[] { android.R.id.text1 });
listView.setAdapter(listAdapter);
setContentView(listView);
}
}
這裡也可以改用自建的資料來源(例如 getData() 回傳大量測試字串)搭配 ArrayAdapter 來驗證滾動與效能表現。
當每一列不只有文字,還需要圖片與按鈕時,改用 SimpleAdapter 並自訂一列的配置檔。資料以 Map 列表的形式提供:
public class ListViewActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(
this, getData(),
R.layout.main,
new String[] { "title", "info", "img", "button1" },
new int[] { R.id.title, R.id.info, R.id.img, R.id.button1 });
setListAdapter(adapter);
}
private List<? extends Map<String, ?>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("info", "google 1");
map.put("img", R.drawable.drive);
map.put("button1", "刪除第1行");
list.add(map);
// ... 依此類推加入更多列 ...
return list;
}
}
對應的單列版面配置,用水平的 LinearLayout 把圖片、文字區塊與按鈕排在一起:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="72px"
android:layout_height="72px"
android:layout_margin="5px" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="22px" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="13px" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
</LinearLayout>
從 ArrayAdapter 到 SimpleCursorAdapter,再到 SimpleAdapter,ListView 透過不同 Adapter 就能靈活適應各種資料型態與版面需求。本文原始作者為 gongzibai。
Android 四大基本組件詳解與生命周期全梳理
Android 四大基本組件分別是:Activity、Service(服務)、Content Provider(內容提供者)與 BroadcastReceiver(廣播接
Android 性能優化:Google 課程之渲染(Render)篇
術語預備 texture(紋理)與 meshes:打個比方,做一個飛機模型,你要先雕刻出立體的飛機模型(mesh),此時它還沒上色,於是你要用一張紙把它包起來、在上面畫上
淺談 Android 中加載高清大圖及圖片壓縮方式(二)
在 Android 中直接加載高清原圖往往會帶來兩個問題:一是解碼和顯示耗時較長,影響用戶體驗;二是大尺寸位圖會佔用大量內存,容易引發內存溢出甚至崩潰。因此,加載大圖時通
使用 Android Studio 建立第一個 Hello World App
建立第一個 Android 專案 幾乎所有程式語言的第一個範例都是 Hello World,Android 也不例外。使用 Android Studio 建立新專案後,輸