sqliteopenhelper - Variable Passing in Android? -
i have variable currentdb
in mainactivity.java
. initiating variable different values depending upon user choosing different tabs. corresponding code :
@override public void tab_selector(view v) { switch (v.getid()) { case r.id.veg_tab: setcontentview(r.layout.vegetable); currentlistview = (listview) findviewbyid(r.id.veg_list); currentdb = "veg"; break; case r.id.meat_tab : setcontentview(r.layout.meat_drawer); currentlistview = (listview) findviewbyid(r.id.meat_list); currentdb = "meat"; break; default: break; } currentlist = mydb.getallitems(currentdb); // definition below, works perfect itemadder2.updatedata(currentlist); currentlistview.setadapter(itemadder2); }
i have method in dbhelper.java
ideally takes table name , item id input , deletes corresponding row specified table.
public integer deleteitem (string dbname, int id) { sqlitedatabase db = this.getwritabledatabase(); return db.delete(dbname, "id = ? ", new string[] { integer.tostring(id) }); }
this 1 more function obtain data table , works perfectly.
public arraylist<string> getallitems(string dbname) { arraylist<string> array_list = new arraylist<string>(); //hp = new hashmap(); sqlitedatabase db = this.getreadabledatabase(); cursor res = db.rawquery( "select * " + dbname, null ); res.movetofirst(); while(res.isafterlast() == false){ array_list.add(res.getstring(res.getcolumnindex(column_name))); res.movetonext(); } return array_list; }
i have created customized listviewadapter
in order display data stored in databases. have provided relevant buttons editing , deletion through onclick()
functionality. now, problem identifying database refer inside listviewadapter
. cannot pass currentdb
information mainactivity.java
listviewadapter.java
. please help. in advance. code snippet listviewadapter.java
follows :
public class listviewadapter extends baseadapter { context mcontext; layoutinflater minflater; arraylist marray; dbhelper mydb; string dbname; public listviewadapter(context context, layoutinflater inflater) { mcontext = context; minflater = inflater; marray = new arraylist(); mydb = new dbhelper(mcontext); dbname = "veg"; // didn't know do, tried initialize manually, din't work :( } @override public int getcount() { return marray.size(); } @override public object getitem(int position) { return marray.get(position); } @override public long getitemid(int position) { // particular data set uses string ids // have put in method return position; } @override public view getview(final int position, view convertview, final viewgroup parent) { viewholder holder; // check if view exists // if so, no need inflate , findviewbyid again! if (convertview == null) { // inflate custom row layout xml. convertview = minflater.inflate(r.layout.list_item, null); // create new "holder" subviews holder = new viewholder(); holder.itemnameview = (textview) convertview.findviewbyid(r.id.item_name); holder.itemexpiryview = (textview) convertview.findviewbyid(r.id.item_expiry); // taking care of buttons holder.editbutton = (button) convertview.findviewbyid(r.id.button_edit); holder.deletebutton = (button) convertview.findviewbyid(r.id.button_delete); // hang onto holder future recycling convertview.settag(holder); } else { // skip expensive inflation/findviewbyid // , holder made holder = (viewholder) convertview.gettag(); } // set listener on buttons holder.editbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mcontext, "edit button clicked", toast.length_short).show(); } }); holder.deletebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { deleteitem(position); // potential problem here?? toast.maketext(mcontext, "item deleted", toast.length_short).show(); } }); // more code after // grab title , author json string name = ""; string expiry = "7 days"; // write appropriate codes obtain values string variables above name = (string) getitem(position); // send these strings textviews display holder.itemnameview.settext(name); holder.itemexpiryview.settext(expiry); return convertview; } // used ever have // inflation , finding id once ever per view private static class viewholder { public textview itemnameview; public textview itemexpiryview; public button editbutton; public button deletebutton; } public void updatedata(arraylist arraypassed) { // update adapter's data set marray = arraypassed; notifydatasetchanged(); } public void deleteitem(int pos){ marray.remove(pos); // ideally, shouldn't there mydb.deleteitem(dbname, pos); // here need pass appropriate dbname // marray = mydb.getallitems(); // ideally should there notifydatasetchanged(); } }
please let me know if need extra. rephrase or topic, tag changes welcome. being noob, not capable how convey. appreciate help. :)
create public method in adapter set db name, eg : in listviewadapter class,
public void setdbname(string dbname){ this.dbname = dbname; }
and on tab_selector(view v) method set db name adapter
eg :
@override public void tab_selector(view v) { switch (v.getid()) { case r.id.veg_tab: setcontentview(r.layout.vegetable); currentlistview = (listview) findviewbyid(r.id.veg_list); currentdb = "veg"; break; case r.id.meat_tab : setcontentview(r.layout.meat_drawer); currentlistview = (listview) findviewbyid(r.id.meat_list); currentdb = "meat"; break; default: break; } currentlist = mydb.getallitems(currentdb); itemadder2.updatedata(currentlist); currentlistview.setadapter(itemadder2); itemadder2.setdbname(currentdb ); }
Comments
Post a Comment