java - Android Content Provider with Multiple Tables -
i using android content provider , have few tables inserting info into. first table has 3 columns , in usecontentprovider class display rows beingness added using toast message @ bottom. working ok. so tried include table has 2 columns (id2, id3) , tried query table , display rows beingness added , whenever error saying failed read row 0, column -1 cursorwindow has 1 rows, 3 columns. not sure doing wrong, help appreciated.
public class usecontentprovideactivity extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // add together 1st course of study contentvalues values = new contentvalues(); values.put(mycontentprovider._id, 1510); values.put(mycontentprovider.name, "jordan"); values.put(mycontentprovider.grade, 9); uri uri = getcontentresolver().insert(mycontentprovider.content_uri, values); contentvalues values2 = new contentvalues(); values2.put(mycontentprovider._id2, 1510); values2.put(mycontentprovider._id3, 9000); uri uri2 = getcontentresolver().insert(mycontentprovider.content_uri2, values); uri2 = getcontentresolver().insert( uri.parse("content://cs.ecl.provider.courses/friend"), values); // query added highschooler toast.maketext(this, "added schooler:", toast.length_short).show(); uri alldescs = uri.parse("content://cs.ecl.provider.courses/highschooler"); cursor cl = managedquery(alldescs, null, null, null, "name"); if (cl.movetofirst()) { { toast.maketext( this, cl.getstring(cl.getcolumnindex(mycontentprovider._id)) + ", " + cl.getstring(cl .getcolumnindex(mycontentprovider.name)) + ", " + cl.getstring(cl .getcolumnindex(mycontentprovider.grade)), toast.length_long).show(); } while (cl.movetonext()); } //2nd table // query added friends toast.maketext(this, "added friend:", toast.length_short).show(); uri alldescs2 = uri.parse("content://cs.ecl.provider.courses/friend"); cursor cl2 = managedquery(alldescs2, null, null, null, "name"); if (cl2.movetofirst()) { { toast.maketext( this, cl2.getstring(cl2.getcolumnindex(mycontentprovider._id2)) + ", " + cl2.getstring(cl2 .getcolumnindex(mycontentprovider._id3)) , toast.length_long).show(); } while (cl2.movetonext()); } getcontentresolver().delete( uri.parse("content://cs.ecl.provider.courses/highschooler"), null, null); }
}
public class mycontentprovider extends contentprovider { public static final string provider_name = "cs.ecl.provider.courses"; // first url public static final uri content_uri = uri.parse("content://" + provider_name + "/highschooler"); // sec url public static final uri content_uri2 = uri.parse("content://" + provider_name + "/friend"); // 3rd url public static final uri content_uri3 = uri.parse("content://" + provider_name + "/like"); public static final string _id = "_id"; public static final string name = "name"; public static final string grade = "grade"; public static final string _id2 = "_id1"; public static final string _id3 = "_id2"; private static final int courses = 1; private static final int course_id = 2; // 2nd private static final int courses2 = 3; private static final int course2_id = 4; private static final urimatcher urimatcher; static { urimatcher = new urimatcher(urimatcher.no_match); urimatcher.adduri(provider_name, "highschooler", courses); urimatcher.adduri(provider_name, "highschooler/#", course_id); // sec table urimatcher.adduri(provider_name, "friend", courses2); urimatcher.adduri(provider_name, "friend/#", course2_id); } // using sqlite database private sqlitedatabase coursesdb; private static final string database_name = "lab2"; private static final string database_table = "highschooler"; // 2nd table private static final string database_table2 = "friend"; private static final int database_version = 1; private static final string database_create = "create table " // + database_table + " (_id integer primary key autoincrement, " + database_table + " (_id integer, " + "name text not null, grade integer);"; private static final string database_create2 = "create table " // + database_table + " (_id integer primary key autoincrement, " + database_table2 + " (_id1 integer, " + "_id2 integer);"; // using sqlite database private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(database_create); db.execsql(database_create2); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists descs"); oncreate(db); } } @override public int delete(uri arg0/* uri */, string arg1/* selection */, string[] arg2/* delectionargs */) { int count = 0; switch (urimatcher.match(arg0)) { case courses: count = coursesdb.delete(database_table, arg1, arg2); break; case course_id: string id = arg0.getpathsegments().get(1); count = coursesdb.delete(database_table, _id + " = " + id + (!textutils.isempty(arg1) ? " , (" + arg1 + ')' : ""), arg2); break; default: throw new illegalargumentexception("unknown uri " + arg0); } getcontext().getcontentresolver().notifychange(arg0, null); homecoming count; } @override public string gettype(uri uri) { switch (urimatcher.match(uri)) { // courses case courses: homecoming "vnd.android.cursor.dir/vnd.ecl.courses "; // 1 course of study case course_id: homecoming "vnd.android.cursor.item/vnd.ecl.courses "; // new stuff // courses case courses2: homecoming "vnd.android.cursor.dir/vnd.ecl.courses2 "; // 1 course of study case course2_id: homecoming "vnd.android.cursor.item/vnd.ecl.courses2 "; default: throw new illegalargumentexception("unsupported uri: " + uri); } } @override public uri insert(uri uri, contentvalues values) { uri _uri = null; switch (urimatcher.match(uri)){ case courses: system.out.println("got insert1"); long _id1 = coursesdb.insert(database_table, "", values); //---if added successfully--- if (_id1 > 0) { _uri = contenturis.withappendedid(content_uri, _id1); getcontext().getcontentresolver().notifychange(_uri, null); } break; case courses2: system.out.println("got insert2"); long _id2 = coursesdb.insert(database_table2, "", values); //---if added successfully--- if (_id2 > 0) { _uri = contenturis.withappendedid(content_uri2, _id2); getcontext().getcontentresolver().notifychange(_uri, null); } break; default: throw new sqlexception("failed insert row " + uri); } homecoming _uri; } @override public boolean oncreate() { context context = getcontext(); databasehelper dbhelper = new databasehelper(context); coursesdb = dbhelper.getwritabledatabase(); homecoming (coursesdb == null) ? false : true; } @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitequerybuilder sqlbuilder = new sqlitequerybuilder(); sqlbuilder.settables(database_table); if (urimatcher.match(uri) == course_id) // -- if getting 1 course of study -- sqlbuilder.appendwhere(_id + " = " + uri.getpathsegments().get(1)); if (sortorder == null || sortorder == "") sortorder = name; cursor cl = sqlbuilder.query(coursesdb, projection, selection, selectionargs, null, null, sortorder); // register watch content uri changes cl.setnotificationuri(getcontext().getcontentresolver(), uri); homecoming cl; } @override public int update(uri uri, contentvalues values, string selection, string[] selectionargs) { int count = 0; switch (urimatcher.match(uri)) { case courses: count = coursesdb.update(database_table, values, selection, selectionargs); break; case course_id: count = coursesdb.update( database_table, values, _id + " = " + uri.getpathsegments().get(1) + (!textutils.isempty(selection) ? " , (" + selection + ')' : ""), selectionargs); break; default: throw new illegalargumentexception("unknown uri " + uri); } getcontext().getcontentresolver().notifychange(uri, null); homecoming count; } }
granted, i'm pretty new contentproviders (just started learning them week), think problem right here:
public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitequerybuilder sqlbuilder = new sqlitequerybuilder(); sqlbuilder.settables(database_table);
you set table database_table
, rather using switch grab occurance of courses2
, urimatcher code when end uri /friend
. it's trying @ highschoolers table, , you're giving invalid column names table (which think why it's failing read "column -1").
so here's how create refer friends table:
public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitequerybuilder sqlbuilder = new sqlitequerybuilder(); switch(urimatcher.match(uri)){ case course_id: // -- if getting 1 course of study -- sqlbuilder.settables(database_table); sqlbuilder.appendwhere(_id + " = " + uri.getpathsegments().get(1)); break; case courses2 sqlbuilder.settables(database_table2); break; default: throw new illegalargumentexception("bad uri"); }
one more note code: seek create variables more descriptive database_table
, database_table2
, or courses
, courses2
. using descriptive variables makes things lot easier remember, code grows (i'm using contentprovider 843 lines, , it's still growing).
like said, i'm new contentproviders, i'm missing few other things improve code, solution gave should plenty code working.
java android
No comments:
Post a Comment