A really simple example.
Here is a really simple but very effective example. Once you have the basics, you can easily handle this.
There are two main parts to using a cursor adapter with SQLite:
1. Create the correct cursor from the database.
In your activity:
SQLiteOpenHelper sqLiteOpenHelper = new SQLiteOpenHelper( context, DATABASE_NAME, null, DATABASE_VERSION); SQLiteDatabase sqLiteDatabase = sqLiteOpenHelper.getReadableDatabase(); String query = "SELECT * FROM clients ORDER BY company_name ASC";
2. Create a custom cursor adapter.
Note. The extension from ResourceCursorAdapter involves using XML to create your views.
public class ClientCursorAdapter extends ResourceCursorAdapter { public ClientCursorAdapter(Context context, int layout, Cursor cursor, int flags) { super(context, layout, cursor, flags); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView name = (TextView) view.findViewById(R.id.name); name.setText(cursor.getString(cursor.getColumnIndex("name"))); TextView phone = (TextView) view.findViewById(R.id.phone); phone.setText(cursor.getString(cursor.getColumnIndex("phone"))); } }
Joshua Pinter Dec 12 '13 at 0:46 2013-12-12 00:46
source share