Android Listview onListItemClick get Sqlite Id -
i displaying list sqlite table. have not used bdhelper class. code how can id.
when click on 1st item, shows 0 in table it's id 1. below code.
sqlitedatabase mydb; try { mydb = openorcreatedatabase(db_name, sqlitedatabase.create_if_necessary, null); mydb.execsql("create table if not exists " + country_table + "(country_id integer primary key," + "country_title text," + "country_status int(11));"); /*mydb.execsql("insert " + country_table +" values "+ "(null,'india',1)," + "(null,'china',1)," + "(null,'australia',1)," + "(null,'japan',1)," + "(null,'germany',1)");*/ cursor c = mydb.rawquery("select country_id,country_title " + country_table + " country_status=1", null); if (c != null) { if (c.movetofirst()) { { int id = c.getint(c.getcolumnindex("country_id")); string name = c.getstring(c.getcolumnindex("country_title")); clist.add(id + ") " + name); } while (c.movetonext()); //int itemcount = clist.size(); //toast.maketext(mainactivity.this, itemcount, toast.length_long).show(); setlistadapter(new arrayadapter<string>(this,android.r.layout.simple_list_item_1, clist)); } } } catch (sqliteexception se) { toast.maketext(mainactivity.this, se.getmessage().tostring(),toast.length_long).show(); } protected void onlistitemclick(listview l, view v, int position, long id) { // todo auto-generated method stub super.onlistitemclick(l, v, position, id); toast.maketext(mainactivity.this,string.valueof(id),toast.length_long).show(); }
please suggest should id fron table , not position.
just change query search id:
cursor c = mydb.rawquery("select country_id,country_title " + country_table + " country_id=" + id, null);
Comments
Post a Comment