android - simplecursoradapter/listview to sqlite database -
hi i'm new database. i've code retrieve sms inbox, sent box , draft phone , show in listview using simplecursor adapter. want save , retrieve listview items (one @ time) of "sent box" sqlite database. can insert data database using edittext values. want method select listview item 1 @ time , save in string[] , insert string[] value database. appreciated. in advance.
code display database items using "listactivity" sqlitedatabase db; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); try { db=openorcreatedatabase("student.db", sqlitedatabase.create_if_necessary, null); cursor c=db.rawquery("select id,name,age stud", null); arraylist<string> list = new arraylist<string>(); int count=c.getcount(); if(c.getcount()>0) { while(c.movetonext()) { list.add(c.getstring(0)+" , "+c.getstring(1)+" , "+c.getstring(2)); } c.close(); toast.maketext(this,"total records: "+count, toast.length_long).show(); arrayadapter<string> adapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1, list); getlistview().setadapter(adapter); } else { toast.maketext(this, "no record found" , toast.length_long).show(); } } catch(exception e) { toast.maketext(this, ""+e, toast.length_long).show(); } } public void ondestroy() { super.ondestroy(); db.close(); } }
sqlite database public class mainactivity extends activity { sqlitedatabase db; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); createdb(); //do insert button btninsert=(button)findviewbyid(r.id.btninsert ); btninsert.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { insert(); } }); button btnclear=(button)findviewbyid(r.id.btnclear ); btnclear.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { clear(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu); createmenu(menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { return menuchoice(item); } private void createmenu(menu menu) { menuitem mnu1 = menu.add(0, 0, 0, "insert"); { mnu1.setalphabeticshortcut('i'); mnu1.seticon(android.r.drawable.ic_input_add); } menuitem mnu2 = menu.add(0, 1, 1, "search"); { mnu2.setalphabeticshortcut('s'); mnu2.seticon(android.r.drawable.ic_search_category_default); } menuitem mnu3 = menu.add(0, 2, 2, "delete"); { mnu3.setalphabeticshortcut('d'); mnu3.seticon(android.r.drawable.ic_delete); } menuitem mnu4 = menu.add(0, 3, 3, "view"); { mnu4.setalphabeticshortcut('d'); mnu4.seticon(android.r.drawable.ic_menu_info_details); } } private boolean menuchoice(menuitem item) { intent intent=new intent(); switch (item.getitemid()) { case 0: insert(); return true; case 1: intent.setclass(mainactivity.this, search.class); startactivity(intent); return true; case 2: intent.setclass(mainactivity.this, search.class); startactivity(intent); return true; case 3: intent.setclass(mainactivity.this, viewrecord.class); startactivity(intent); return true; } return false; } public void createdb() { db=openorcreatedatabase("student.db", sqlitedatabase.create_if_necessary, null); db.setlocale(locale.getdefault()); db.setlockingenabled(true); db.setversion(1); string sql="create table if not exists stud(id integer primary key autoincrement, name text, age integer)"; db.execsql(sql); } public void insert() { edittext txtname=(edittext)findviewbyid(r.id.txtname); edittext txtage=(edittext)findviewbyid(r.id.txtage); if(txtname.gettext().tostring().equals("")) { toast.maketext(mainactivity.this, "enter name.", toast.length_short).show(); } else if (txtage.gettext().tostring().equals("")) { toast.maketext(mainactivity.this, "enter age.", toast.length_short).show(); } else { string sql="insert stud(name,age) values('"+ txtname.gettext().tostring() +"',"+txtage.gettext().tostring()+")"; db.execsql(sql); clear(); toast.maketext(mainactivity.this, "record inserted.", toast.length_short).show(); } } public void clear() { edittext txtname=(edittext)findviewbyid(r.id.txtname); edittext txtage=(edittext)findviewbyid(r.id.txtage); txtname.settext(""); txtage.settext(""); txtname.clearfocus(); txtage.clearfocus(); txtname.requestfocus(); } @override public void ondestroy() { super.ondestroy(); db.close(); } }
code displaying sms inbox, sent box , draft public class messagebox extends activity implements onclicklistener { // gui widget button btnsent, btninbox, btndraft; textview lblmsg, lblno; listview lvmsg; // cursor adapter simplecursoradapter adapter; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.messagebox); // init gui widget btninbox = (button) findviewbyid(r.id.btninbox); btninbox.setonclicklistener(this); btnsent = (button) findviewbyid(r.id.btnsentbox); btnsent.setonclicklistener(this); btndraft = (button) findviewbyid(r.id.btndraft); btndraft.setonclicklistener(this); lvmsg = (listview) findviewbyid(r.id.lvmsg); } @override public void onclick(view v) { if (v == btninbox) { // create inbox box uri uri inboxuri = uri.parse("content://sms/inbox"); // list required columns string[] reqcols = new string[] { "_id", "address", "body" }; // content resolver object, deal content // provider contentresolver cr = getcontentresolver(); // fetch inbox sms message built-in content provider cursor c = cr.query(inboxuri, reqcols, null, null, null); // attached cursor adapter , display in listview adapter = new simplecursoradapter(this, r.layout.row, c, new string[] { "body", "address" }, new int[] { r.id.lblmsg, r.id.lblnumber }); lvmsg.setadapter(adapter); } if (v == btnsent) { // create sent box uri uri senturi = uri.parse("content://sms/sent"); // list required columns string[] reqcols = new string[] { "_id", "address", "body" }; // content resolver object, deal content // provider contentresolver cr = getcontentresolver(); // fetch sent sms message built-in content provider cursor c = cr.query(senturi, reqcols, null, null, null); // attached cursor adapter , display in listview adapter = new simplecursoradapter(this, r.layout.row, c, new string[] { "body", "address" }, new int[] { r.id.lblmsg, r.id.lblnumber }); lvmsg.setadapter(adapter); } if (v == btndraft) { // create draft box uri uri drafturi = uri.parse("content://sms/draft"); // list required columns string[] reqcols = new string[] { "_id", "address", "body" }; // content resolver object, deal content // provider contentresolver cr = getcontentresolver(); // fetch sent sms message built-in content provider cursor c = cr.query(drafturi, reqcols, null, null, null); // attached cursor adapter , display in listview adapter = new simplecursoradapter(this, r.layout.row, c, new string[] { "body", "address" }, new int[] { r.id.lblmsg, r.id.lblnumber }); lvmsg.setadapter(adapter); } } }
you getting sms data following code
string[] reqcols = new string[] { "_id", "address", "body" }; // content resolver object, deal content // provider contentresolver cr = getcontentresolver(); // fetch sent sms message built-in content provider cursor c = cr.query(senturi, reqcols, null, null, null);
now need data cursor , insert in database either using raw query doing or using content provider.
for more information read get inbox messages android device show in custom listview
Comments
Post a Comment