Retriveing Data From Exisint Sqlite Database
I wish to store it in arraylist and then display it out in listView. Is there any solution to it ? how do I set the adapter? how can I link this two up and display it into a listvi
Solution 1:
Do you want to display all of data?
Think about using CursorLoader
with SimpleCursorAdapter
. It will load your data once activity is created in background. And in your activity class just implement interface LoaderManager.LoaderCallback
, and in onCreate
you simply initialize Loader with getSupportLoaderManager().initLoader(LOADER_ID, null, this)
Here is the link with good example http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html
EDIT:
This code use in onCreate
// init DB
db = newDB(this);
db.open();
// forming columns from DB to viewsString[] from = newString[] { KEY_ROWID, KEY_USERNAME, KEY_AGE...}; //array of columns from DB
int[] to = new int[] { R.id.textviewId, R.id.textviewUserName, R.id.textviewAge }; //Array of of view components// create adapter
scAdapter = newSimpleCursorAdapter(this, R.layout.item, null, from, to, 0);
ListView lvData = (ListView) findViewById(R.id.lvData); // listview where you want to display data
lvData.setAdapter(scAdapter);
//init loadergetSupportLoaderManager().initLoader(0, null, this);
//implement these mothods with interface `LoaderManager.LoaderCallback<Cursor>`@OverridepublicLoader<Cursor> onCreateLoader(int id, Bundle bndl) {
returnnewMyCursorLoader(this, db);
}
@OverridepublicvoidonLoadFinished(Loader<Cursor> loader, Cursor cursor) {
scAdapter.swapCursor(cursor);
}
@OverridepublicvoidonLoaderReset(Loader<Cursor> loader) {
}
This code must works for you.
EDIT2
staticclassMyCursorLoaderextendsCursorLoader {
DB db;
publicMyCursorLoader(Context context, DB db) {
super(context);
this.db = db;
}
@Overridepublic Cursor loadInBackground() {
return db.getAllData();
}
}
One detail added. This is a class where you download your data from db and return filled cursor.
Post a Comment for "Retriveing Data From Exisint Sqlite Database"