編輯:關於 Android 程式設計
在 Android 的列表開發中,當數據來源是資料庫查詢得到的 Cursor 時,SimpleCursorAdapter 是最省事的幫手。它能夠把游標中的某一列直接映射到 ListView 每個項目的自定義布局上,省去大量手動取值、手動 setText 的繁瑣工作。
構造一個 SimpleCursorAdapter,本質上需要準備五樣東西:當前上下文、列表項的布局資源、一個 Cursor,以及兩個長度一致的數組——前者存放要取用的列名,後者存放對應要填充的控件 ID。二者一一對應,系統便會自動完成「列 → 控件」的綁定。
// 第一步:從資料庫讀取數據
dbHelper = new DBHelper(HistoryOrderActivity.this);
database = dbHelper.getWritableDatabase();
cursor = database.rawQuery("SELECT * FROM " + DBHelper.TABLE_ORDER + " where feedbackTime is not null", null);
// startManagingCursor(cursor); 已被拋棄的方法,作用是把 cursor 的生命週期交給 Activity 管理
String[] fromColumns = new String[] {
"orderDescription", "orderEffectiveTime", "orderConsumeTime",
"promotion", "feedbackInfo", "feedbackTime",
};
int[] toLayoutIDs = new int[] {
R.id.description, R.id.effectiveTime, R.id.consumeTime,
R.id.promotion, R.id.feedbackInfo, R.id.feedbackTime,
};
adapter = new SimpleCursorAdapter(this, R.layout.histortyorder, cursor, fromColumns, toLayoutIDs, 0);
這段代碼把查詢結果裡的六個字段依次填入布局中的六個 TextView,一行列表就輕鬆呈現出來了。
當默認的綁定邏輯無法滿足需求——例如時間戳需要格式化、某些字段需要特殊處理時,我們可以繼承 SimpleCursorAdapter 並重寫 bindView 方法,把填充邏輯牢牢掌握在自己手裡。
// 第一步:從資料庫讀取數據
dbHelper = new DBHelper(HistoryOrderActivity.this);
database = dbHelper.getWritableDatabase();
cursor = database.rawQuery("SELECT * FROM " + DBHelper.TABLE_ORDER + " where feedbackTime is not null", null);
// startManagingCursor(cursor); 已被拋棄的方法,作用是把 cursor 的生命週期交給 Activity 管理
String[] fromColumns = new String[] {
"orderDescription", "orderEffectiveTime", "orderConsumeTime",
"promotion", "feedbackInfo", "feedbackTime",
};
int[] toLayoutIDs = new int[] {
R.id.description, R.id.effectiveTime, R.id.consumeTime,
R.id.promotion, R.id.feedbackInfo, R.id.feedbackTime,
};
if (cursor == null) {
return;
}
adapter = new HistoryOrderAdapter(HistoryOrderActivity.this, R.layout.histortyorder, cursor, fromColumns, toLayoutIDs, 0);
下面是自定義適配器的實現。核心在於重寫 bindView,在其中自行完成控件查找與數據賦值:
public class HistoryOrderAdapter extends SimpleCursorAdapter {
private Cursor m_cursor;
private Context m_context;
private LayoutInflater miInflater;
public HistoryOrderAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
m_context = context;
m_cursor = c;
}
@Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
View convertView = null;
if (arg0 == null) {
convertView = miInflater.inflate(R.layout.histortyorder, null);
} else {
convertView = arg0;
}
TextView tv_Description = (TextView) convertView.findViewById(R.id.description);
TextView tv_EffectiveTime = (TextView) convertView.findViewById(R.id.effectiveTime);
TextView tv_ConsumeTime = (TextView) convertView.findViewById(R.id.consumeTime);
TextView tv_promotion = (TextView) convertView.findViewById(R.id.promotion);
TextView tv_FeedbackInfo = (TextView) convertView.findViewById(R.id.feedbackInfo);
TextView tv_FeedbackTime = (TextView) convertView.findViewById(R.id.feedbackTime);
tv_Description.setText(arg2.getString(arg2.getColumnIndex("orderDescription")));
tv_EffectiveTime.setText(ShopUtils.changeTimestampToTime(Long.valueOf(arg2.getString(arg2.getColumnIndex("orderEffectiveTime")))));
tv_ConsumeTime.setText(ShopUtils.changeTimestampToTime(Long.valueOf(arg2.getString(arg2.getColumnIndex("orderConsumeTime")))));
tv_promotion.setText(arg2.getString(arg2.getColumnIndex("promotion")));
tv_FeedbackInfo.setText(arg2.getString(arg2.getColumnIndex("feedbackInfo")));
tv_FeedbackTime.setText(ShopUtils.changeTimestampToTime(Long.valueOf(arg2.getString(arg2.getColumnIndex("feedbackTime")))));
}
}
需要說明的是,上面的示例仍顯得有些粗糙,比如 miInflater 並未在構造方法中初始化,實戰時應補上 LayoutInflater.from(context),並善用 ViewHolder 模式緩存控件引用以減少重複的 findViewById 開銷。這樣既能發揮 SimpleCursorAdapter 的便捷,又能兼顧性能與可讀性。
UC-Android 逆向工程師面試第 2 題分析
題目背景 本題以 UC-crackme-2.apk 為例,考察 Android 逆向工程師對 Java 層、Native 層與動態分析工具的綜合理解。相比基礎題,它增加了
Android Launcher 機制解析:添加快捷方式與桌面小部件的完整流程
研究 Launcher 模塊兩個月下來,最大的體會是:Launcher 本身其實沒做太多事,它把大量工作交给了框架與各應用程式提供的標準介面。很多乍看之下神奇的功能,讀過
Android 資料儲存:SharedPreferences 用法詳解
本文將介紹如何使用 SharedPreferences 儲存與讀取資料,並說明三種取得物件的方式與檔案的存放位置。 簡介 SharedPreferences 是資料儲存中
AsyncTask 實戰:為什麼你的非同步任務老是慢半拍
關於 AsyncTask 的基本用法,官方文件與〈深入理解多線程 AsyncTask〉已經講得很清楚,這裡直接進入實務問題。 一個令人費解的现象 前陣子遇到一個很奇怪的