android - CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 happens when record is deleted -
a understand in cursor, can't figure out solution own. need help. , happens when delete record. on displaycontact.java in line 65.
here class files.
displaycontact.java
import java.text.simpledateformat; import java.util.calendar; import android.app.activity; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.view.keyevent; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.widget.toast; public class displaycontact extends activity { int from_where_i_am_coming = 0; private dbhelper mydb ; textview name ; textview note; int id_to_update = 0; button btndel; string formatteddate; textview txtview; simpledateformat date; calendar c; // textview dosage; // textview day; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_display_contact); name = (textview) findviewbyid(r.id.edittextname); note = (textview) findviewbyid(r.id.edittextnote); btndel = (button) findviewbyid(r.id.buttondelete); mydb = new dbhelper(this); c = calendar.getinstance(); date = new simpledateformat("yyyy-mm-dd"); formatteddate = date.format(c.gettime()); txtview = new textview(this); bundle extras = getintent().getextras(); if(extras !=null) { int value = extras.getint("id"); if(value>0){ //means view part not add contact part. cursor rs = mydb.getdata(value); id_to_update = value; rs.movetofirst(); string nam = rs.getstring(rs.getcolumnindex(dbhelper.contacts_column_name)); string not = rs.getstring(rs.getcolumnindex(dbhelper.contacts_column_note)); string cd = rs.getstring(rs.getcolumnindex(dbhelper.contacts_column_date)); if (!rs.isclosed()) { rs.close(); } name.settext((charsequence)nam); note.settext((charsequence)not); txtview.settext(formatteddate); } //////////////////////////////////////////////////////////////////////////////////////////// onclicklistener listenerdel = new onclicklistener() { public void onclick(view v) { mydb.deletecontact(id_to_update); toast.maketext(getapplicationcontext(), "deleted successfully", toast.length_short).show(); intent intent = new intent(getapplicationcontext(),com.example.database3.mainactivity.class); startactivity(intent); finish(); } }; btndel.setonclicklistener(listenerdel); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. bundle extras = getintent().getextras(); if(extras !=null) { int value = extras.getint("id"); if(value>0){ getmenuinflater().inflate(r.menu.display_contact, menu); } else{ getmenuinflater().inflate(r.menu.mainmenu, menu); } } return true; } public void run(view view) { bundle extras = getintent().getextras(); if(extras !=null) { int value = extras.getint("id"); if(value>0){ if(mydb.updatecontact(id_to_update,name.gettext().tostring(), note.gettext().tostring(), txtview.gettext().tostring())){ toast.maketext(getapplicationcontext(), "updated", toast.length_short).show(); intent intent = new intent(getapplicationcontext(),com.example.database3.mainactivity.class); startactivity(intent); } else{ toast.maketext(getapplicationcontext(), "not updated", toast.length_short).show(); } } else{ if(mydb.insertcontact(name.gettext().tostring(), note.gettext().tostring(), txtview.gettext().tostring())){ toast.maketext(getapplicationcontext(), "added", toast.length_short).show(); } else{ toast.maketext(getapplicationcontext(), "not done", toast.length_short).show(); } intent intent = new intent(getapplicationcontext(),com.example.database3.mainactivity.class); startactivity(intent); } } } @override public boolean onoptionsitemselected(menuitem item) { super.onoptionsitemselected(item); switch(item.getitemid()){ case r.id.item1: bundle databundle = new bundle(); databundle.putint("id", 0); intent intent = new intent(getapplicationcontext(),com.example.database3.displaycontact.class); intent.putextras(databundle); startactivity(intent); bundle extras = getintent().getextras(); int value = extras.getint("id"); if(value>0){ if(mydb.updatecontact(id_to_update,name.gettext().tostring(), note.gettext().tostring(), txtview.gettext().tostring())){ toast.maketext(getapplicationcontext(), "updated", toast.length_short).show(); } else{ toast.maketext(getapplicationcontext(), "not updated", toast.length_short).show(); } } else{ if(mydb.insertcontact(name.gettext().tostring(), note.gettext().tostring(), txtview.gettext().tostring())){ toast.maketext(getapplicationcontext(), "added", toast.length_short).show(); } else{ toast.maketext(getapplicationcontext(), "not done", toast.length_short).show(); } } finish(); return true; default: return super.onoptionsitemselected(item); } } @override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { intent = new intent(this,mainactivity.class); a.addflags(intent.flag_activity_clear_top); startactivity(a); return true; } return super.onkeydown(keycode, event); } }
dbhelper.java
import java.util.arraylist; //import java.util.hashmap; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.databaseutils; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class dbhelper extends sqliteopenhelper { public static final string database_name = "mydbname.db"; public static final string contacts_table_name = "contacts"; public static final string contacts_column_id = "id"; public static final string contacts_column_name = "name"; public static final string contacts_column_note = "note"; public static final string contacts_column_date = "date"; // private hashmap hp; public dbhelper(context context) { super(context, database_name , null, 1); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql( "create table contacts " + "(id integer primary key," + " name text," + " note text," + " date text)" ); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists contacts"); oncreate(db); } public boolean insertcontact (string name, string note, string date) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put("name", name); contentvalues.put("date", date); contentvalues.put("note", note); db.insert("contacts", null, contentvalues); return true; } public cursor getdata(int id){ sqlitedatabase db = this.getreadabledatabase(); cursor res = db.rawquery( "select * contacts id="+id+"", null ); return res; } public int numberofrows(){ sqlitedatabase db = this.getreadabledatabase(); int numrows = (int) databaseutils.querynumentries(db, contacts_table_name); return numrows; } public boolean updatecontact (integer id, string name, string note, string date){ sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put("name", name); contentvalues.put("note", note); contentvalues.put("date", date); db.update("contacts", contentvalues, "id = ? ", new string[] { integer.tostring(id) } ); return true; } public integer deletecontact (integer id){ sqlitedatabase db = this.getwritabledatabase(); return db.delete("contacts", "id = ? ", new string[] { integer.tostring(id) }); } public arraylist <string> getallcotacts(){ arraylist<string> array_list = new arraylist<string>(); //hp = new hashmap(); sqlitedatabase db = this.getreadabledatabase(); cursor res = db.rawquery( "select * contacts", null ); res.movetofirst(); while(res.isafterlast() == false){ array_list.add(res.getstring(res.getcolumnindex(contacts_column_date)) + " " + res.getstring(res.getcolumnindex(contacts_column_name))); res.movetonext(); } return array_list; } }
mainactivity.java
import java.util.arraylist; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.keyevent; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.listview; public class mainactivity extends activity { public final static string extra_message = "com.example.database3.message"; private listview obj; dbhelper mydb; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mydb = new dbhelper(this); arraylist <string> array_list = mydb.getallcotacts(); arrayadapter <string> arrayadapter = new arrayadapter<string>(this,android.r.layout.simple_list_item_1, array_list); //adding list view. obj = (listview)findviewbyid(r.id.listview1); obj.setadapter(arrayadapter); obj.setonitemclicklistener(new onitemclicklistener(){ @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub int id_to_search = arg2 + 1; bundle databundle = new bundle(); databundle.putint("id", id_to_search); intent intent = new intent(getapplicationcontext(),com.example.database3.displaycontact.class); intent.putextras(databundle); startactivity(intent); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.mainmenu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item){ super.onoptionsitemselected(item); switch(item.getitemid()) { case r.id.item1: bundle databundle = new bundle(); databundle.putint("id", 0); intent intent = new intent(getapplicationcontext(),com.example.database3.displaycontact.class); intent.putextras(databundle); startactivity(intent); return true; default: return super.onoptionsitemselected(item); } } public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { movetasktoback(true); } return super.onkeydown(keycode, event); } }
i think rs returns 0 try code
cursor rs = mydb.getdata(value); if (rs != null && rs.movetofirst()) { rs.movetofirst(); (int = 0; < rs.getcount(); i++) { string nam = rs.getstring(rs.getcolumnindex(dbhelper.contacts_column_name)); string not = rs.getstring(rs.getcolumnindex(dbhelper.contacts_column_note)); string cd = rs.getstring(rs.getcolumnindex(dbhelper.contacts_column_date)); cur.movetonext(); } } } } catch (exception e) { e.printstacktrace(); } { if ( mydb != null) { mydb.close(); mydb = null; } if (rs != null) { rs.close(); rs = null; } }
Comments
Post a Comment