How to read an SQLite DB in android with a cursorloader? -


i'm setting app people can create groups of friends. when group created, writes 2 tables sql database. first table has group name , group id. second table has 2 columns, group id , user id. working fine.

however, want able read database. i'm using listview fragment cursorloader i'm having trouble getting information display. want list group names first table in list view.

my problem that, when first used cursorloader list contacts, using uri content provider in oncreateloader method. had content_uri contactscontracts.contacts class.

example of cursorloader contentprovider:

@override public loader<cursor> oncreateloader(int i, bundle bundle) {     uri contenturi = contactscontract.contacts.content_uri;     return new cursorloader(getactivity(),contenturi,projection,selection,args,order); } 

however, without using content provider, don't know put in oncreateloader method because return new cursorloader(...) requires uri in second argument.

any suggestion on how might able display database data in listview?

fragment class code:

public class grouplistfragment extends listfragment implements loadermanager.loadercallbacks<cursor> {  cursoradapter madapter; private onitemselectedlistener listener; private static final string[] projection ={groupcontract.groupdetails.column_name_group_name}; private static final string selection = null; final string[] = {groupcontract.groupdetails.column_name_group_name}; final int[] = {android.r.id.text1}; private static final string[] args = null; private static final string order = null; private cursor c;   @override public void oncreate(bundle savedinstancestate){     super.oncreate(savedinstancestate);     madapter = new simplecursoradapter(getactivity(), android.r.layout.simple_list_item_1,null,from,to,0 );     readdbasync readdb = new readdbasync();     readdb.execute(); }  @override public void onactivitycreated(bundle savedinstancestate){     super.onactivitycreated(savedinstancestate);     setlistadapter(madapter);     getloadermanager().initloader(0,null,this); }  @override public loader<cursor> oncreateloader(int i, bundle bundle) {     uri contenturi = uri.parse("content://preamble.oneapp");     uri tableuri = uri.withappendedpath(contenturi,groupcontract.groupdetails.table_name);     return new cursorloader(getactivity(),tableuri,projection,selection,args,order); }  @override public void onloadfinished(loader<cursor> cursorloader, cursor cursor) {     madapter.swapcursor(cursor); }  @override public void onloaderreset(loader<cursor> cursorloader) {     madapter.swapcursor(null); }   private class readdbasync extends asynctask<void,void,string> {      @override     protected string doinbackground(void... voids) {          contractdbhelpers mdbhelper = new contractdbhelpers(getactivity());         sqlitedatabase db = mdbhelper.getreadabledatabase();         string returnvalue = "database read";         c = db.query(groupcontract.groupdetails.table_name,projection,null,null,null,null,null);         return returnvalue;     }      @override     protected void onpostexecute(string result){         toast.maketext(getactivity(), result, toast.length_long).show();     } }  } 

these steps create cursorloader in list fragment

1) create class extending sqliteopenhelper , override oncreate , onupgrade create tables.

2) create class extending contentprovider , create uris access database. refer http://developer.android.com/guide/topics/providers/content-providers.html. add uris urimatcher use in oncreate, onupdate, query, etc (overridden methods) match uri. refer http://developer.android.com/reference/android/content/urimatcher.html

3) in insert method call getcontext().getcontentresolver().notifychange(uri, null). in query method call setnotificationuri(contentresolver cr, uri uri) before returning content provider insertion change reflect automatically loader. (https://stackoverflow.com/a/7915117/936414).

4) give uri in oncreateloader.

note: without content provider, automatic refreshing of changes list not feasible of current android version. if don't want have contentprovider visible, set exported attribute in manifest false. or can implement custom cursorloader in https://stackoverflow.com/a/7422343/936414 retrieve data database. in case automatic refreshing of data not possible


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -