Cursor Adapter and SQLite Example

Cursor adapter and sqlite example

Really simple example.

Here is a really simple, but very effective, example. Once you have the basics down you can easily build off of it.

There are two main parts to using a Cursor Adapter with SQLite:

  1. Create a proper Cursor from the Database.

  2. Create a custom Cursor Adapter that takes the Cursor data from the database and pairs it with the View you intend to represent the data with.

1. Create a proper 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: Extending from ResourceCursorAdapter assumes you use 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")));
}
}

Populating a listView with SQLite database using simple cursor adapter

Problem solved. I found the answer in developer.android.com in Content Provider section:

Note: A provider isn't required to have a primary key, and it isn't required to use _ID as the column name of a primary key if one is present. However, if you want to bind data from a provider to a ListView, one of the column names has to be _ID. This requirement is explained in more detail in the section Displaying query results.

Hear is the Original link:

http://developer.android.com/guide/topics/providers/content-provider-basics.html

show data from sqlite to listview using cursor adapter

Change getDetails() of EmployeeDatabase with below code

public Cursor getDetails()
{
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("select rowid _id,name, age,time from employeedetailnew", null);
}

and change ShowData.java's onCreate() as below,

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EmployeeDatabase empClick = new EmployeeDatabase(getApplicationContext());
Cursor cursor = empClick.getDetails();
if(cursor !=null)
getListView().setAdapter(
new android.support.v4.widget.SimpleCursorAdapter(EmptyClass.this, R.layout.shows_view, cursor, new String[] {
"name", "age", "time"
}, new int[] {R.id.name1,R.id.age2,R.id.time3}, 0));
}

and go through SimpleCursorAdapter and this example

Refresh SQLite data on ListView with CursorAdapter

Maybe you can try to use Loaders.

They let's you bind the data source (Content Provider or other) with the Activity or Fragment. While Loaders are active they should monitor the source of their data and deliver new results when the contents change.

I'll give you a general approach to how implement them (CursorLoader in particular) :

public class MachineInfo extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor> {

private static final int LOADER_INTEGER = 1;

// ... existing code

@Override
public void onActivityCreated(Bundle savedInstanceState) {
getLoaderManager().initLoader(LOADER_INTEGER, null, this);
super.onActivityCreated(savedInstanceState);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(
this,
ContentProvider.CONTENT_URI,
projection,
null,
null,
sortOrder
);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mCursorAdapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
}

You can read a lot on internet about this, a good start is for example here.

Hope this helps



Related Topics



Leave a reply



Submit