android - column '0' does not exist for ContentProvider SQLite -
i getting error shown below, how fix problem?
fatal exception: main java.lang.illegalargumentexception: column '0' not exist com.example.contentproviderexample.mainactivity.onloadfinished(mainactivity.java:62)
the stack trace shows error exists on line 62 of onloadfinished method of mainactivity
@override public void onloadfinished(loader<cursor> loader, cursor cursor) { adapter.swapcursor(cursor); // <-- stacktrace error }
here code providerexample class extends contentprovider
public class providerexample extends contentprovider { public static final string database_name = "notes.db"; public static final int database_version = 1; public static final string table_name = "notes"; public static final string authority = "com.example.contentproviderexample.providerexample"; public static final urimatcher surimatcher; private static final int notes = 1; private static final int notes_id = 2; private static hashmap<string, string> notesprojectionmap; static { surimatcher = new urimatcher(urimatcher.no_match); surimatcher.adduri(authority, table_name, notes); surimatcher.adduri(authority, table_name + "/#", notes_id); } public static interface noteitems extends basecolumns { // notes in content_uri plural actual table name singular version of word public static final uri content_uri = uri.parse("content://" + authority + "/notes"); public static final string _id = "_id"; public static final string title = "title"; public static final string text = "text"; public static final string content_type = contentresolver.cursor_dir_base_type + "/vnd.example.providerexample"; // static final string single_record = "vnd.android.cursor.item/vnd.example.providerexample"; public static final string content_item_type = contentresolver.cursor_item_base_type + "/vnd.example.providerexample"; // static final string multiple_records = "vnd.android.cursor.dir/vnd.example.providerexample"; public static final string[] projection_all = {_id, title, text}; public static final string sort_order_default = title + " asc"; } 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 table if not exists " + table_name + " (" + noteitems._id + " integer primary key autoincrement, " + noteitems.title + " text, " + noteitems.text + " text);"); } @override public void onupgrade(sqlitedatabase db, int previousversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } } private databasehelper dbhelper; @override public boolean oncreate() { dbhelper = new databasehelper(getcontext()); return true; } @override public string gettype(uri uri) { switch (surimatcher.match(uri)){ case notes: return noteitems.content_type; case notes_id: return noteitems.content_item_type; default: throw new illegalargumentexception("unknown uri" + uri); } }
here mainactivity class
public class mainactivity extends activity implements loadermanager.loadercallbacks<cursor>{ private simplecursoradapter adapter; private loadermanager loadermanager; private cursorloader cursorloader; private listview listview; private int primarykey; private string primarykeystring; private string testtitle; private string testtext; private static final int loader_id = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); primarykey = 0; testtitle = "test title"; testtext = "test text"; providerexample providerexample = new providerexample(); primarykeystring = string.valueof(primarykey); getloadermanager().initloader(loader_id, null, this); adapter = new simplecursoradapter(this, r.layout.row_layout, null, new string[]{primarykeystring, testtitle, testtext}, new int[]{r.id.textview1, r.id.textview2, r.id.textview3}, adapter.no_selection); listview = (listview) findviewbyid(r.id.listview1); listview.setadapter(adapter); // load database test values //loaddatabase(); } // end oncreate @override public loader<cursor> oncreateloader(int id, bundle args) { string[] projection = {providerexample.noteitems._id, providerexample.noteitems.title, providerexample.noteitems.text }; cursorloader = new cursorloader(this, providerexample.noteitems.content_uri, projection, null, null, null); return cursorloader; } @override public void onloadfinished(loader<cursor> loader, cursor cursor) { adapter.swapcursor(cursor); } @override public void onloaderreset(loader<cursor> loader) { adapter.swapcursor(null); } }
edit:
added here query method code
@override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitequerybuilder qb = new sqlitequerybuilder(); qb.settables(table_name); qb.setprojectionmap(notesprojectionmap); switch(surimatcher.match(uri)){ case notes: break; case notes_id: selection = selection + "_id=" + uri.getlastpathsegment(); break; default: throw new illegalargumentexception("unknown uri" + uri); } sqlitedatabase db = dbhelper.getreadabledatabase(); cursor c = qb.query(db, noteitems.projection_all, selection, selectionargs, null, null, sortorder); c.setnotificationuri(getcontext().getcontentresolver(), uri); return c; }
Comments
Post a Comment