Cursor adapter and sqlite example

Hi I'm looking for sample code that uses a cursor adapter with sqlite?

+50
android android-layout database sqlite android-cursoradapter
Mar 28 '11 at 10:32
source share
3 answers

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:

  • Create the correct Cursor from the database.

  • Create your own Cursor Adapter that takes the Cursor data from the database and compares it with the View that you are going to represent the data from.

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"; // No trailing ';' Cursor cursor = sqLiteDatabase.rawQuery(query, null); ClientCursorAdapter adapter = new ClientCursorAdapter( this, R.layout.clients_listview_row, cursor, 0 ); this.setListAdapter(adapter); 

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"))); } } 
+91
Dec 12 '13 at 0:46
source share

On Android, how to use a cursor with a raw query in sqlite:

 Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM mytable " + "where Age > 10 LIMIT 5", null); if (c != null ) { if (c.moveToFirst()) { do { String firstName = c.getString(c.getColumnIndex("FirstName")); int age = c.getInt(c.getColumnIndex("Age")); results.add("" + firstName + ",Age: " + age); }while (c.moveToNext()); } } c.close(); 
+35
Jan 08 2018-12-12T00:
source share

CursorAdapter example with Sqlite

 ... DatabaseHelper helper = new DatabaseHelper(this); aListView = (ListView) findViewById(R.id.aListView); Cursor c = helper.getAllContacts(); CustomAdapter adapter = new CustomAdapter(this, c); aListView.setAdapter(adapter); ... class CustomAdapter extends CursorAdapter { // CursorAdapter will handle all the moveToFirst(), getCount() logic for you :) public CustomAdapter(Context context, Cursor c) { super(context, c); } public void bindView(View view, Context context, Cursor cursor) { String id = cursor.getString(0); String name = cursor.getString(1); // Get all the values // Use it however you need to TextView textView = (TextView) view; textView.setText(name); } public View newView(Context context, Cursor cursor, ViewGroup parent) { // Inflate your view here. TextView view = new TextView(context); return view; } } private final class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "db_name"; private static final int DATABASE_VERSION = 1; private static final String CREATE_TABLE_TIMELINE = "CREATE TABLE IF NOT EXISTS table_name (_id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar);"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_TIMELINE); db.execSQL("INSERT INTO ddd (name) VALUES ('One')"); db.execSQL("INSERT INTO ddd (name) VALUES ('Two')"); db.execSQL("INSERT INTO ddd (name) VALUES ('Three')"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public Cursor getAllContacts() { String selectQuery = "SELECT * FROM table_name; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); return cursor; } } 
+16
May 4 '13 at 5:54
source share



All Articles