Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

安卓開發實例:以 ListActivity 綁定資料庫數據

編輯:關於 Android 開發

本文示範兩種在 ListActivity 中綁定數據的做法:其一以 SimpleCursorAdapter 動態綁定 SQLite 資料庫,其二以 SimpleAdapter 靜態綁定。

做法一:SimpleCursorAdapter 動態綁定

版面檔 bookmark3.xml 中,特別留意 ListView 的 id 需為 @id/android:list,並額外配置一個空資料時顯示的 TextView:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="8dp" android:paddingRight="8dp"> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" android:drawSelectorOnTop="false"/> <TextView android:id="@+id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" android:text="No data"/> </LinearLayout>

BookMarks3.java 透過 DatabaseHelper 取得可讀資料庫,查詢 bookmarks 資料表後,用 Cursor 產生 SimpleCursorAdapter 並綁定到 ListActivity:

 package com.cx_browser; import com.cx_browser.db.DatabaseHelper; import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class BookMarks3 extends ListActivity{ private static final String DATABASE_NAME="test_db4"; private static final String TABLE_NAME="bookmarks"; private static final String TABLE_ID="_id"; private static final String TABLE_TITLE="title"; private static final String TABLE_URL="url"; @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // 此處可改為從資料庫取出對應網址 } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bookmarks3); DatabaseHelper dbHelper=new DatabaseHelper(BookMarks3.this,DATABASE_NAME); SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor cursor=db.query(TABLE_NAME,new String[]{TABLE_ID,TABLE_TITLE,TABLE_URL},null,null,null,null,null); int count = cursor.getCount(); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_expandable_list_item_1, cursor, new String[] {TABLE_TITLE}, new int[] {android.R.id.text1}); setListAdapter(adapter); } }

關鍵在於 SimpleCursorAdapter 的建構參數:Context、行版型、Cursor、要綁定的資料欄位陣列,以及對應到版型中控件的 id 陣列。

做法二:SimpleAdapter 靜態綁定

版面檔 a3.xml 定義了 name 與 ip 兩個 TextView:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="8dip" android:paddingRight="8dip" android:paddingBottom="8dip"> <TextView android:id="@+id/name" android:textSize="16sp" android:layout_width="180dip" android:layout_height="25dip"/> <TextView android:id="@+id/ip" android:textSize="16sp" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>

Activity3.java 以多個 HashMap 組成 ArrayList,再用 SimpleAdapter 綁定:

 package com.helloworld; import java.util.ArrayList; import java.util.HashMap; import android.app.ListActivity; import android.os.Bundle; import android.widget.SimpleAdapter; public class Activity3 extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>(); HashMap<String,String> map1=new HashMap<String,String>(); map1.put("name", "chenxi"); map1.put("ip", "192.168.1.0"); HashMap<String,String> map2=new HashMap<String,String>(); map2.put("name", "zhoujielun"); map2.put("ip", "192.168.1.1"); list.add(map1); list.add(map2); SimpleAdapter listAdapter=new SimpleAdapter(this, list, R.layout.a3, new String[]{"name","ip"}, new int[]{R.id.name,R.id.ip}); setListAdapter(listAdapter); } }

兩種做法各有適用情境:需要動態讀取資料庫時用 SimpleCursorAdapter,數據來源為記憶體集合時則用 SimpleAdapter,可依需求靈活選用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved