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

SimpleCursorAdapter 實戰:從直接綁定到自定義擴展

編輯:關於 Android 程式設計

在 Android 的列表開發中,當數據來源是資料庫查詢得到的 Cursor 時,SimpleCursorAdapter 是最省事的幫手。它能夠把游標中的某一列直接映射到 ListView 每個項目的自定義布局上,省去大量手動取值、手動 setText 的繁瑣工作。

一、SimpleCursorAdapter 的基本用法

構造一個 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

當默認的綁定邏輯無法滿足需求——例如時間戳需要格式化、某些字段需要特殊處理時,我們可以繼承 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 的便捷,又能兼顧性能與可讀性。

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