Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android之自定義SimpleCursorAdapter的使用

android之自定義SimpleCursorAdapter的使用

編輯:關於Android編程

SimpleCursorAdapter直接使用的方法:

SimpleCursorAdapter允許你綁定一個游標的列到ListView上,並使用自定義的layout顯示每個項目。

SimpleCursorAdapter的創建,需要傳入當前的上下文、一個layout資源,一個游標和兩個數組:一個包含使用的列的名字,另一個(相同大小)數組包含View中的資源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);

SimpleCursorAdapter自定義的使用:

//第一步:從數據庫讀取數據
		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);

適配器的實現:
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")))));
	}
}

HistoryOrderAdapter有點糙,需要改進。



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved