java - Two ArrayList in one ListView in Android -
please me in putting 2 arraylist in 1 listview only. display 2 columns database using 2 listview. im not in programming need guys. in advance
this main activity
public class mainactivity extends activity { datadb data = new datadb(); listview list; listview list2; arrayadapter<string> listadapter; public mainactivity() { } protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); list = (listview) findviewbyid(r.id.listview); list2 = (listview) findviewbyid(r.id.listview2); // set data arraylist<string> firstname = new arraylist<string>(); try { firstname = data.getfnamedb(this); } catch (sqlexception e) { e.printstacktrace(); } listadapter = new arrayadapter<string>(this, r.layout.support_simple_spinner_dropdown_item, firstname); // set adapter list.setadapter(listadapter); arraylist<string> lastname = new arraylist<string>(); try { lastname = data.getlnamedb(this); } catch (sqlexception e) { e.printstacktrace(); } listadapter = new arrayadapter<string>(this, r.layout.support_simple_spinner_dropdown_item, lastname); // set adapter list2.setadapter(listadapter); }
this datadb
public class datadb { databasehelper con; arraylist<string> firstname = new arraylist<string>(); arraylist<string> lastname = new arraylist<string>(); public datadb() { } public arraylist<string> getfnamedb(context context) throws sqlexception { this.con = new databasehelper(context); try { this.con.createdatabase(); } catch (ioexception e) { ; } if (!this.con.checkdatabase()) { return null; } else { this.con.opendatabase(); sqlitedatabase db = this.con.getwritabledatabase(); (cursor cursor = db.rawquery("select firstname tbl_doctor", (string[]) null); cursor.movetonext(); this.firstname.add(cursor.getstring(0))) { } this.con.close(); return this.firstname; } } public arraylist<string> getlnamedb(context context) throws sqlexception { this.con = new databasehelper(context); try { this.con.createdatabase(); } catch (ioexception e) { ; } if (!this.con.checkdatabase()) { return null; } else { this.con.opendatabase(); sqlitedatabase db = this.con.getwritabledatabase(); (cursor cursor = db.rawquery("select lastname tbl_doctor", (string[]) null); cursor.movetonext(); this.lastname.add(cursor.getstring(0))) { } this.con.close(); return this.lastname; } }
just clear things out , should use single listview
, display both firstname , lastname in that, demonstrate code , add essential parts understand things better way,
in method accessing firstname
, can update both firstname
, lastname
in single query below ->
public arraylist<string> getnamedb(context context) throws sqlexception { this.con = new databasehelper(context); try { this.con.createdatabase(); } catch (ioexception e) { ; } if (!this.con.checkdatabase()) { return null; } else { this.con.opendatabase(); sqlitedatabase db = this.con.getwritabledatabase(); (cursor cursor = db.rawquery("select firstname,lastname tbl_doctor", (string[]) null); cursor.movetonext(); this.firstname.add(cursor.getstring(0)+" "+cursor.getstring(1)) { } this.con.close(); return this.firstname; } }
now can use list in adapter below,
arraylist<string> names = new arraylist<string>(); try { names = data.getnamedb(this); } catch (sqlexception e) { e.printstacktrace(); } listadapter = new arrayadapter<string>(this, r.layout.support_simple_spinner_dropdown_item, names); // set adapter list.setadapter(listadapter);
ask if need further help..
Comments
Post a Comment