Use of custom SimpleCursorAdapter for android

Source: Internet
Author: User

Use of custom SimpleCursorAdapter for android
SimpleCursorAdapter:

SimpleCursorAdapter allows you to bind a cursor column to the ListView and display each item using custom layout.

To create SimpleCursorAdapter, You need to input the current context, a layout resource, a cursor, and two Arrays: one containing the name of the column used, and the other (the same size) array containing the resource ID in the View, displays the data values of corresponding columns.

// Step 1: read data from the database dbHelper = new DBHelper (HistoryOrderActivity. this); database = dbHelper. getWritableDatabase (); cursor = database. rawQuery ("SELECT * FROM" + DBHelper. TABLE_ORDER + "where feedbackTime is not null", null); // startManagingCursor (cursor); abandoned method, it mainly submits the life cycle of cursor to Activity management String [] fromColumns = new String [] {"orderDescription", "orderinclutivetime", "orderConsumeTime", "promotion", "feedbackInfo ", "feedbackTime",}; int [] toLayoutIDs = new int [] {R. id. description, R. id. specified tivetime, R. id. consumeTime, R. id. promotion, R. id. feedbackInfo, R. id. feedbackTime}; adapter = new SimpleCursorAdapter (this, R. layout. histortyorder, cursor, fromColumns, toLayoutIDs, 0 );

SimpleCursorAdapter custom usage:
// Step 1: read data from the database dbHelper = new DBHelper (HistoryOrderActivity. this); database = dbHelper. getWritableDatabase (); cursor = database. rawQuery ("SELECT * FROM" + DBHelper. TABLE_ORDER + "where feedbackTime is not null", null); // startManagingCursor (cursor); abandoned method, it mainly submits the life cycle of cursor to Activity management String [] fromColumns = new String [] {"orderDescription", "orderinclutivetime", "orderConsumeTime", "promotion", "feedbackInfo ", "feedbackTime",}; int [] toLayoutIDs = new int [] {R. id. description, R. id. specified tivetime, 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 );

Adapter implementation:
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;}@Overridepublic 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 is a bit rough and needs to be improved.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.