android - SQLite ListView CursorIndexOutofBoundsException: Index -1 requested, with a size of 0 -
i succeeded in setting listview populated pairs of latitudes , longitudes sqlite database. wrote code tap 1 of these pairs call google maps , navigate there user's current location. , when held click, got dialogfragment asking me if wanted delete latlang pair; if user confirmed, pair deleted sqlite database, removed list, , list repopulated.
after getting working well, tried rewrite code include column in sqlite database hold string labels latlang pairs. now, when click latlang pair, instead of opening google maps intent, nothing happens. , when hold click , confirm delete, app crashes cursorindexoutofboundsexception. i've tried debugging, reading further this, , checking stackoverflow archives, i'm still not sure what's going on.
if sees obvious please let me know. i'm still new programming in general.
here sqlite databasehandler class:
package com.detimil.breadcrumbs1; import java.util.arraylist; import java.util.list; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class databasehandler extends sqliteopenhelper { // static variables // database version private static final int database_version = 1; // database name private static final string database_name = "breadcrumbsmanager"; // contacts table name private static final string table_breadcrumbs = "breadcrumbs"; // contacts table columns names private static final string key_id = "id"; private static final string key_label = "longitude"; private static final string key_latitude = "latitude"; private static final string key_longitude = "longitude"; public databasehandler(context context) { super(context, database_name, null, database_version); } // creating tables @override public void oncreate(sqlitedatabase db) { string create_breadcrumbs_table = "create table " + table_breadcrumbs + "(" + key_id + " integer primary key," + key_label + " text," + key_latitude + " text," + key_longitude + " text" + ")"; db.execsql(create_breadcrumbs_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed db.execsql("drop table if exists " + table_breadcrumbs); // create tables again oncreate(db); } /** * crud(create, read, update, delete) operations */ // adding new location/breadcrumb public void addbreadcrumb(breadcrumb breadcrumb) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_label, breadcrumb.getbreadcrumblabel()); // label values.put(key_latitude, breadcrumb.getbreadcrumblatitude()); // latitude values.put(key_longitude, breadcrumb.getbreadcrumblongitude()); // longitude // inserting row db.insert(table_breadcrumbs, null, values); db.close(); // closing database connection } // getting single breadcrumb public breadcrumb getbreadcrumb(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_breadcrumbs, new string[] { key_id, key_label, key_latitude, key_longitude }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); //(i edited snippet per advice comment below. unfortunately, code still not run correctly.) if (cursor.getcount() > 0){ cursor.movetofirst(); } breadcrumb breadcrumb = new breadcrumb(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getint(2), cursor.getint(3)); // return breadcrumb cursor.close(); return breadcrumb; } // getting breadcrumbs public list<breadcrumb> getallbreadcrumbs() { list<breadcrumb> breadcrumblist = new arraylist<breadcrumb>(); // select query string selectquery = "select * " + table_breadcrumbs; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { breadcrumb breadcrumb = new breadcrumb(); breadcrumb.setid(integer.parseint(cursor.getstring(0))); breadcrumb.setbreadcrumblabel(cursor.getstring(1)); breadcrumb.setbreadcrumblatitude(cursor.getint(2)); breadcrumb.setbreadcrumblongitude(cursor.getint(3)); // adding location list breadcrumblist.add(breadcrumb); } while (cursor.movetonext()); } // return locations list cursor.close(); return breadcrumblist; } // getting breadcrumb labels (leaving out lats , langs) public list<breadcrumb> getallbreadcrumblabels() { list<breadcrumb> breadcrumblabellist = new arraylist<breadcrumb>(); // select query string selectquery = "select * " + table_breadcrumbs; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { breadcrumb breadcrumb = new breadcrumb(); breadcrumb.setid(integer.parseint(cursor.getstring(0))); breadcrumb.setbreadcrumblabel(cursor.getstring(2)); // adding label list breadcrumblabellist.add(breadcrumb); } while (cursor.movetonext()); } // return locations list cursor.close(); return breadcrumblabellist; } // getting breadcrumbs/locations count public int getbreadcrumbscount() { string countquery = "select * " + table_breadcrumbs; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); cursor.close(); // return count return cursor.getcount(); } //updating single location public int updatebreadcrumbs(breadcrumb breadcrumb) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_label, breadcrumb.getbreadcrumblabel()); values.put(key_latitude, breadcrumb.getbreadcrumblatitude()); values.put(key_longitude, breadcrumb.getbreadcrumblongitude()); //updating row return db.update(table_breadcrumbs, values, key_id + " = ?", new string[] { string.valueof(breadcrumb.getid()) }); } // deleting single location public void deletebreadcrumb(breadcrumb breadcrumb) { sqlitedatabase db = this.getwritabledatabase(); int id = breadcrumb.getid(); system.out.println("comment deleted id: " + id); db.delete(table_breadcrumbs, key_id + " = " + id, null); db.close(); } }
and here activity listview:
package com.detimil.breadcrumbs1; import java.util.list; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.net.uri; import android.os.bundle; import android.util.log; import android.view.view; import android.view.window; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.adapterview.onitemlongclicklistener; import android.widget.arrayadapter; import android.widget.listview; public class collectedbreadcrumbsactivity extends activity { private listview listview; private databasehandler db; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_collected_breadcrumbs); db = new databasehandler(this); list<breadcrumb> breadcrumbs = db.getallbreadcrumblabels(); listview = (listview) findviewbyid(r.id.list); arrayadapter<breadcrumb> adapter = new arrayadapter<breadcrumb>(this, android.r.layout.simple_list_item_1, breadcrumbs); listview.setadapter(adapter); this.listview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> l, view v, int position, long id) { arrayadapter<breadcrumb> adapter = (arrayadapter<breadcrumb>) listview.getadapter(); if (listview.getadapter().getcount() > 0) { breadcrumb breadcrumb = db.getbreadcrumb(position); final intent intent = new intent(intent.action_view,uri.parse("http://maps.google.com/maps" + "&daddr=" + breadcrumb.getbreadcrumblatitude()/1e6 + "," + breadcrumb.getbreadcrumblongitude()/1e6)); intent.setclassname("com.google.android.apps.maps","com.google.android.maps.mapsactivity"); startactivity(intent);}}}); this.listview.setonitemlongclicklistener(new onitemlongclicklistener() { public boolean onitemlongclick(adapterview<?> l, view v, final int position, long id) { log.d("hellolistview", "you clicked item: " + id + " @ position:" + position); log.d("hellolistview", "number of items in adapter:" + listview.getadapter().getcount()); alertdialog.builder alertbox = new alertdialog.builder(collectedbreadcrumbsactivity.this); // set message display alertbox.setmessage("delete breadcrumb?"); // set positive/yes button , create listener alertbox.setpositivebutton("delete", new dialoginterface.onclicklistener() { // when button clicked public void onclick(dialoginterface arg0, int arg1) { @suppresswarnings("unchecked") arrayadapter<breadcrumb> adapter = (arrayadapter<breadcrumb>) listview.getadapter(); if (listview.getadapter().getcount() > 0) { breadcrumb listedbreadcrumb = (breadcrumb) listview.getadapter().getitem(position); breadcrumb breadcrumb = db.getbreadcrumb(position); db.deletebreadcrumb(breadcrumb); adapter.remove(listedbreadcrumb); } adapter.notifydatasetchanged(); } }); // set negative/no button , create listener alertbox.setnegativebutton("cancel", new dialoginterface.onclicklistener() { // when button clicked public void onclick(dialoginterface arg0, int arg1) { } }); alertbox.show(); return true; } }); listview.setemptyview(findviewbyid(r.id.empty)); } }
the sqlite database populated in first place through clicking button in mainactivity. below snippet of code shows happens when button pressed: initiates activity, , (in theory?) creates sqlite row consisting of string label, latitude expressed int, , longitude expressed int.
public void dropbreadcrumb(view view) { intent intent = new intent(this, droppedcrumb.class); databasehandler db = new databasehandler(this); string label = "breadcrumb"+db.getbreadcrumbscount()+1; breadcrumb_latitude = (breadcrumb_latitude * 1e6); int lat = (int)breadcrumb_latitude; breadcrumb_longitude = (breadcrumb_longitude * 1e6); int lng = (int)breadcrumb_longitude; db.addbreadcrumb(new breadcrumb(label, lat, lng)); startactivity(intent); }
here actual error read when hold listview item , confirm want delete it. app crashes...
05-19 14:05:25.512: d/hellolistview(2086): clicked item: 0 @ position:0 05-19 14:05:25.512: d/hellolistview(2086): number of items in adapter:4 05-19 14:05:25.602: i/adreno200-eglsub(2086): <configwindowmatch:2087>: format rgba_8888. 05-19 14:05:26.653: w/dalvikvm(2086): threadid=1: thread exiting uncaught exception ( (group=0x40e4c438) 05-19 14:05:26.663: e/androidruntime(2086): fatal exception: main 05-19 14:05:26.663: e/androidruntime(2086): android.database.cursorindexoutofboundsexception: index -1 requested, size of 0 05-19 14:05:26.663: e/androidruntime(2086): @ android.database.abstractcursor.checkposition(abstractcursor.java:418) 05-19 14:05:26.663: e/androidruntime(2086): @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:136) 05-19 14:05:26.663: e/androidruntime(2086): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:50) 05-19 14:05:26.663: e/androidruntime(2086): @ com.detimil.breadcrumbs1.databasehandler.getbreadcrumb(databasehandler.java:84) 05-19 14:05:26.663: e/androidruntime(2086): @ com.detimil.breadcrumbs1.collectedbreadcrumbsactivity$2$1.onclick(collectedbreadcrumbsactivity.java:72) 05-19 14:05:26.663: e/androidruntime(2086): @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:167) 05-19 14:05:26.663: e/androidruntime(2086): @ android.os.handler.dispatchmessage(handler.java:99) 05-19 14:05:26.663: e/androidruntime(2086): @ android.os.looper.loop(looper.java:137) 05-19 14:05:26.663: e/androidruntime(2086): @ android.app.activitythread.main(activitythread.java:4950) 05-19 14:05:26.663: e/androidruntime(2086): @ java.lang.reflect.method.invokenative(native method) 05-19 14:05:26.663: e/androidruntime(2086): @ java.lang.reflect.method.invoke(method.java:511) 05-19 14:05:26.663: e/androidruntime(2086): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1004) 05-19 14:05:26.663: e/androidruntime(2086): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:771) 05-19 14:05:26.663: e/androidruntime(2086): @ dalvik.system.nativestart.main(native method)
if have thoughts i'd grateful. please let me know if other piece of information/code useful you.
thank much.
edit/update:
i edited getbreadcrumb method per user nikhil's initial suggestion below, primary problem still there. here error messages get...
05-20 17:48:48.966: d/hellolistview(15738): clicked item: 2 @ position:2 05-20 17:48:48.966: d/hellolistview(15738): number of items in adapter:4 05-20 17:48:49.056: i/adreno200-eglsub(15738): <configwindowmatch:2087>: format rgba_8888. 05-20 17:48:50.297: w/dalvikvm(15738): threadid=1: thread exiting uncaught exception (group=0x414db438) 05-20 17:48:50.297: e/androidruntime(15738): fatal exception: main 05-20 17:48:50.297: e/androidruntime(15738): android.database.cursorindexoutofboundsexception: index -1 requested, size of 0 05-20 17:48:50.297: e/androidruntime(15738): @ android.database.abstractcursor.checkposition(abstractcursor.java:418) 05-20 17:48:50.297: e/androidruntime(15738): @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:136) 05-20 17:48:50.297: e/androidruntime(15738): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:50) 05-20 17:48:50.297: e/androidruntime(15738): @ com.detimil.breadcrumbs1.databasehandler.getbreadcrumb(databasehandler.java:86) 05-20 17:48:50.297: e/androidruntime(15738): @ com.detimil.breadcrumbs1.collectedbreadcrumbsactivity$2$1.onclick(collectedbreadcrumbsactivity.java:72) 05-20 17:48:50.297: e/androidruntime(15738): @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:167) 05-20 17:48:50.297: e/androidruntime(15738): @ android.os.handler.dispatchmessage(handler.java:99) 05-20 17:48:50.297: e/androidruntime(15738): @ android.os.looper.loop(looper.java:137) 05-20 17:48:50.297: e/androidruntime(15738): @ android.app.activitythread.main(activitythread.java:4950) 05-20 17:48:50.297: e/androidruntime(15738): @ java.lang.reflect.method.invokenative(native method) 05-20 17:48:50.297: e/androidruntime(15738): @ java.lang.reflect.method.invoke(method.java:511) 05-20 17:48:50.297: e/androidruntime(15738): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1004) 05-20 17:48:50.297: e/androidruntime(15738): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:771) 05-20 17:48:50.297: e/androidruntime(15738): @ dalvik.system.nativestart.main(native method)
you having 2 columns same name longitude
private static final string key_id = "id"; private static final string key_label = "longitude"; private static final string key_latitude = "latitude"; private static final string key_longitude = "longitude";
Comments
Post a Comment