Monday, 15 July 2013

android - Expandablelistview adapter from phpmyadmin -



android - Expandablelistview adapter from phpmyadmin -

i'd create expandable listview in android of product's list grouped category. i'm using phpmyadmin database. how can show kid based on categories? using jsonparser & php code here.

category 1 - product - product b

category 2 - product c - product d

this php code show header (category)

<?php include("koneksi.php"); $q = mysql_query('select * kategori_barang'); $v = '{"info" : ['; while($r=mysql_fetch_array($q)) { $ob = array("<br>","<b>","</b>"); if(strlen($v)<12) { $v .= '{"id_kat" : "'.$r['id_kat'].'", "kat_barang" : "'.$r['kat_barang'].'"}'; } else { $v .= '{"id_kat" : "'.$r['id_kat'].'", "kat_barang" : "'.$r['kat_barang'].'"}'; } } $v .= ']}'; echo $v; ?>

here database

`product_id` int(4) not null auto_increment, `cat_id` int(4) not null default '0', `subcat_id` int(4) not null default '0', `product_name` varchar(50) not null default '', `product_price` int(8) not null default '0', `product_weight` int(4) not null default '0', `product_weight_unit` varchar(10) not null default '', `product_price_unit` varchar(10) not null default '', `product_image` varchar(250) default null, `product_in_stock` int(8) default null,

here adapter

public class expandablelistadapter extends baseexpandablelistadapter { private context _context; private list<string> _listdataheader; // header titles // kid info in format of header title, kid title private hashmap<string, list<string>> _listdatachild; public expandablelistadapter(context context, list<string> listdataheader, hashmap<string, list<string>> listchilddata) { this._context = context; this._listdataheader = listdataheader; this._listdatachild = listchilddata; } @override public object getchild(int groupposition, int childposition) { homecoming this._listdatachild.get(this._listdataheader.get(groupposition)) .get(childposition); } @override public long getchildid(int groupposition, int childposition) { // todo auto-generated method stub homecoming childposition; } @override public view getchildview(int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { final string childtext = (string) getchild(groupposition, childposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.list_item, null); } textview txtlistchild = (textview) convertview .findviewbyid(r.id.lblitem); txtlistchild.settext(childtext); homecoming convertview; } @override public int getchildrencount(int groupposition) { homecoming this._listdatachild.get(this._listdataheader.get(groupposition)) .size(); } @override public object getgroup(int groupposition) { homecoming this._listdataheader.get(groupposition); } @override public int getgroupcount() { // todo auto-generated method stub homecoming this._listdataheader.size(); } @override public long getgroupid(int groupposition) { // todo auto-generated method stub homecoming 0; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { string headertitle = (string) getgroup(groupposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.list_group, null); } textview lblheader = (textview) convertview .findviewbyid(r.id.lblheader); lblheader.settypeface(null, typeface.bold); lblheader.settext(headertitle); homecoming convertview; } @override public boolean hasstableids() { // todo auto-generated method stub homecoming false; } @override public boolean ischildselectable(int arg0, int arg1) { // todo auto-generated method stub homecoming true; }

and here mainactivity

public class mainactivity extends activity { string link_url = ""; string [] str=null; public static final string ar_id_kat = "id_kat"; public static final string ar_kat_barang = "kat_barang"; jsonarray str_login = null; jsonobject jsonobject; jsonarray jsonarray; arraylist<hashmap<string, string>> daftar_buku = new arraylist<hashmap<string, string>>(); progressdialog mprogressdialog; expandablelistadapter listadapter; expandablelistview explistview; list<string> listdataheader; arraylist<hashmap<string, string>> arraylist; hashmap<string, list<string>> listdatachild; string id_kat, kat_barang; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // listview explistview = (expandablelistview) findviewbyid(r.id.lvexp); // preparing list info preparelistdata(); listadapter = new expandablelistadapter(this, listdataheader, listdatachild); // setting list adapter explistview.setadapter(listadapter); // listview grouping click listener explistview.setongroupclicklistener(new ongroupclicklistener() { @override public boolean ongroupclick(expandablelistview parent, view v, int groupposition, long id) { // toast.maketext(getapplicationcontext(), // "group clicked " + listdataheader.get(groupposition), // toast.length_short).show(); homecoming false; } }); // listview grouping expanded listener explistview.setongroupexpandlistener(new ongroupexpandlistener() { @override public void ongroupexpand(int groupposition) { toast.maketext(getapplicationcontext(), listdataheader.get(groupposition) + " expanded", toast.length_short).show(); } }); // listview grouping collasped listener explistview.setongroupcollapselistener(new ongroupcollapselistener() { @override public void ongroupcollapse(int groupposition) { toast.maketext(getapplicationcontext(), listdataheader.get(groupposition) + " collapsed", toast.length_short).show(); } }); // listview on kid click listener explistview.setonchildclicklistener(new onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) { // todo auto-generated method stub toast.maketext( getapplicationcontext(), listdataheader.get(groupposition) + " : " + listdatachild.get( listdataheader.get(groupposition)).get( childposition), toast.length_short) .show(); homecoming false; } }); } private class downloadjson extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); // create progressdialog mprogressdialog = new progressdialog(mainactivity.this); // set progressdialog title mprogressdialog.settitle("sharani designs"); // set progressdialog message mprogressdialog.setmessage("loading..."); mprogressdialog.setindeterminate(false); // show progressdialog mprogressdialog.show(); } @override protected void doinbackground(void... params) { /*arraylist = new arraylist<hashmap<string, string>>(); // retrieve json objects given url address arraylist<hashmap<string, string>> contactlist = new arraylist<hashmap<string, string>>(); jsonobject = jsonparser .getjsonfromurl("http://api.androidhive.info/contacts/"); */ jsonparser jparser = new jsonparser(); link_url = "http://10.0.2.2/login/test_kat.php"; jsonobject json = jparser.ambiljson(link_url); seek { str_login = json.getjsonarray("info"); (int = 0; < str_login.length(); i++) { jsonobject ar = str_login.getjsonobject(i); string id = ar.getstring(ar_id_kat); string isb = ar.getstring(ar_kat_barang); hashmap<string, string> map = new hashmap<string, string>(); // adding each kid node hashmap key => value map.put(ar_id_kat, id); map.put(ar_kat_barang, isb); daftar_buku.add(map); } } grab (jsonexception e) { e.printstacktrace(); } // todo auto-generated method stub homecoming null; } @override protected void onpostexecute(void args) { // locate listview in listview_main.xml mprogressdialog.dismiss(); } }

android mysql expandablelistview

No comments:

Post a Comment