編輯:關於 Android 開發
本文示範兩種在 ListActivity 中綁定數據的做法:其一以 SimpleCursorAdapter 動態綁定 SQLite 資料庫,其二以 SimpleAdapter 靜態綁定。
版面檔 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 陣列。
版面檔 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,可依需求靈活選用。
Android 插件化(一):改進 MultiDex 動態載入 assets 中的 apk
Android 插件化(一):改進 MultiDex 動態載入 assets 中的 apk 為了解決方法數超過 65535 的問題,Google 建議用 MultiDex
HBase Java 編程實戰入門示例
HBase Java 編程示例詳解 HelloWorld.zip 下面這份 HelloWorld 示例展示了如何用 Java 操作 HBase,涵蓋了連接建立、建表、刪表
Android 搜尋功能實作:SQLite 保存搜尋歷史與即時篩選
搜尋框不只是輸入框 在許多 App 中,搜尋功能不只是把關鍵字送出,還需要記住使用者最近搜尋過的內容。適當的歷史紀錄能減少重複輸入,也能讓使用者快速回到感興趣的主題。本文
在 Android 中使用 SQLite:從建表到增刪改查的完整示範
SQLite 輕量、體積小,幾乎成了行動端的預設選擇。Android 系統本身就內建了 SQLite,不像在 Cocos2d-x 中還得自己塞外部庫,用起來相當省事。本文