Android - Content Provider query cursor -



Android - Content Provider query cursor -

i cannot seem grasp concept of content provider. think bloats code, over-complicates it, , plainly awful.

btw first illustration code of multi-table contentprovider find on web. there absolutelly none other, except maybe contactsprovider ("veeeerrrrry usefull" - beingness super-stuffed , indescifrable). sure many noobs me appreciate this.

please help me create simple cursor content provider. have normal code bellow (using simple sqliteopenhelper), , need translate content-provider code:

button btnp = (button) findviewbyid(r.id.btnproc); btnp.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { context mycontext = getapplicationcontext(); sqlitedatabase db = new mydbhelper(mycontext).getwritabledatabase(); string sql = "select _id,data,titlu,cont notificari date(data)<=date(datetime())"; cursor cursor = db.rawquery(sql, null); log.w("...mainactivity...", "record count:"+integer.tostring(curu.getcount())); while(curu.movetonext()){ string lista = curu.getstring(0)+",'"+curu.getstring(1)+"',"+curu.getstring(2)+",'"+ curu.getstring(3)+"'"; log.e("...mainactivity...", lista); } } });

so need cursor obtained content-provider. have started developing "mind-blowing" content-provider, have no thought if doing ok, since overcomplicated. here goes (my content-provider needs refference more 1 table):

public class myfirstprovider extends contentprovider { static final string provider_name = "com.mystuff.myapp.myfirstprovider"; static final string url = "content://" + provider_name + "/cte"; static final string url2 = "content://" + provider_name + "/clienti"; static final string url3 = "content://" + provider_name + "/uzeri"; static final string url4 = "content://" + provider_name + "/notificari"; static final uri content_uri = uri.parse(url); static final uri content_uri2 = uri.parse(url2); static final uri content_uri3 = uri.parse(url3); static final uri content_uri4 = uri.parse(url4); static final int uricode = 1; static final int uricode1 = 2; static final int uricode2 = 3; static final int uricode3 = 4; static final int uricode4 = 5; static final int uricode5 = 6; static final int uricode6 = 7; static final int uricode7 = 8; static final urimatcher urimatcher; private static hashmap<string, string> values; static { urimatcher = new urimatcher(urimatcher.no_match); urimatcher.adduri(provider_name, "cte", uricode); urimatcher.adduri(provider_name, "cte/*", uricode1); urimatcher.adduri(provider_name, "clienti", uricode2); urimatcher.adduri(provider_name, "clienti/*", uricode3); urimatcher.adduri(provider_name, "uzeri", uricode4); urimatcher.adduri(provider_name, "uzeri/*", uricode5); urimatcher.adduri(provider_name, "notificari", uricode6); urimatcher.adduri(provider_name, "notificari/*", uricode7); } @override public boolean oncreate() { context context = getcontext(); databasehelper dbhelper = new databasehelper(context); db = dbhelper.getwritabledatabase(); if (db != null) { homecoming true; } homecoming false; } @override public cursor query(uri uri, string[] projection, string selection,string[] selectionargs, string sortorder) { sqlitequerybuilder qb = new sqlitequerybuilder(); switch (urimatcher.match(uri)) { case uricode: qb.settables(table_name); qb.setprojectionmap(values); break; case uricode2: qb.settables(table_clienti); qb.setprojectionmap(values); if (sortorder == null || sortorder == "") { sortorder = tcl_nume_client; } break; case uricode6: qb.settables(table_notif); qb.setprojectionmap(values); break; default: throw new illegalargumentexception("unsupported uri: " + uri); } cursor c = qb.query(db, projection, selection, selectionargs, null,null, sortorder); c.setnotificationuri(getcontext().getcontentresolver(), uri); homecoming c; } // ... there other code here regarding insert,update , delete (not relevant now) private sqlitedatabase db; static final string database_name = "mydb"; static final string table_name = "names"; public static final string table_uzeri = "uzeri"; public static final string table_notif = "notificari"; static final int database_version = 3; static final string create_db_table = " create table " + table_name + " (_id integer primary key autoincrement, " + " name text not null);"; static final string create_db_table_notificari = "create table if not exists " + table_notif + " (_id integer primary key autoincrement, " + tnot_id_not + " integer not null default 0, " + tnot_data + " date not null, " + tnot_sursa + " integer not null default 0, "+ tnot_dest + " integer not null default 0, "+ tnot_tip + " text, "+ tnot_titlu + " text, "+ tnot_titlucont + " text, "+ tnot_cont + " text, "+ tnot_sters + " integer default 0, "+ tnot_nou + " integer not null default 0)"; private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(create_db_table); db.execsql(create_db_table_uzeri); db.execsql(create_db_table_notificari); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); db.execsql("drop table if exists " + table_uzeri); db.execsql("drop table if exists " + table_notificari); oncreate(db); } }

i tried "translate" activity code logs content of cursor nothing. it's frustrating. here "translation":

button btnp = (button) findviewbyid(r.id.btnproc); btnp.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string[] coloane = {myfirstprovider.tnot_id_id, myfirstprovider.tnot_data,myfirstprovider.tnot_titlu, myfirstprovider.tnot_cont}; cursor curu = mainactivity.this.getcontentresolver().query(myfirstprovider.content_uri_notif,coloane,null,null,myfirstprovider.tnot_data); log.w("...mainactivity...", "recorduri:"+integer.tostring(curu.getcount())); while(curu.movetonext()){ string lista = curu.getstring(0)+",'"+curu.getstring(1)+"',"+curu.getstring(2)+",'"+ curu.getstring(3)+"'"; log.e("...mainactivity...", lista); } } });

i must assure table has 4 records within (i insert them manually after table creation) if set code within contentprovider, log displays content of table desired:

cursor c = qb.query(db, projection, selection, selectionargs, null,null, sortorder); while(c.movetonext()){ log.w("...provider...",c.getstring(0)+"-"+c.getstring(1)+"-"+c.getstring(2)); }

but not want in content provider, want in activity code. doing wrong?

please help.

thank you

you don't have utilize contentprovider if don't have to. identify requirements. if info private application , if has stored in table format, can access sqlite engine straight in android without having go via contentprovider. if want other apps or scheme access app's data, yes - that's legitimate utilize case contentprovider.

nonetheless, suggest read article - tells how access sqlite engine , without contentprovider.

hope helps.

anyways, problem @ hand, seek -

cursor c = qb.query(db, projection, selection, selectionargs, null,null, sortorder); if (c == null) { // error - log message } else if (c.getcount() < 1) { // nil show - log message } else { while(c.movetonext()){ log.w("...provider...",c.getstring(0)+"-"+c.getstring(1)+"-"+c.getstring(2)); } }

android android-contentprovider android-cursor

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -