android - How to filter listview data from PHP JSON Parser -
how can filter data using textview in listview , data php using json parser. searched net don't point because using array of string while im using parsed data php. help.
this code
productlist.java import java.util.arraylist; import java.util.hashmap; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.listactivity; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.text.editable; import android.text.textwatcher; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.edittext; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; public class productlist extends listactivity { // progress dialog private progressdialog pdialog; private edittext et; int textlength = 0; private static final string url = "http://192.168.254.101/productlist.php"; private static final string tag_posts = "message"; private static final string tag_brand = "brand"; private static final string tag_category = "category"; private static final string tag_description = "description"; private static final string tag_code = "code"; private static final string tag_quantity = "quantity"; private static final string tag_unit = "unit"; private static final string tag_unitprice = "unitprice"; private jsonarray mcomments = null; //manages of our comments in list. private arraylist<hashmap<string, string>> mcommentlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.productlist); } @override protected void onresume() { // todo auto-generated method stub super.onresume(); //loading comments via asynctask new loadcomments().execute(); } public void updatejsondata() { mcommentlist = new arraylist<hashmap<string, string>>(); jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(url); try { mcomments = json.getjsonarray(tag_posts); (int = 0; < mcomments.length(); i++) { jsonobject c = mcomments.getjsonobject(i); //gets content of each tag string brand = c.getstring(tag_brand); string category = c.getstring(tag_category); string description = c.getstring(tag_description); string code = c.getstring(tag_code); string quantity = c.getstring(tag_quantity); string unit = c.getstring(tag_unit); string unitprice = c.getstring(tag_unitprice); hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_brand, brand); map.put(tag_category, category); map.put(tag_description, description); map.put(tag_code, code); map.put(tag_quantity, quantity); map.put(tag_unit, unit); map.put(tag_unitprice, unitprice); mcommentlist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } } private void updatelist() { listadapter adapter = new simpleadapter(this, mcommentlist, r.layout.single_post, new string[] { tag_brand, tag_category, tag_description, tag_code, tag_quantity, tag_unit, tag_unitprice}, new int[]{ r.id.brand, r.id.category, r.id.description, r.id.code, r.id.quantity, r.id.unit, r.id.price }); setlistadapter(adapter); listview lv = getlistview(); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { } }); } public class loadcomments extends asynctask<void, void, boolean> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(productlist.this); pdialog.setmessage("loading products..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected boolean doinbackground(void... arg0) { updatejsondata(); return null; } @override protected void onpostexecute(boolean result) { super.onpostexecute(result); pdialog.dismiss(); updatelist(); } } }
productlist.xml
android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" > <linearlayout android:id="@+id/top_layover" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:orientation="vertical" > <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edittext01" android:layout_gravity="center_horizontal" android:hint="search.." /> </linearlayout> <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/bottom_layover" android:layout_below="@+id/top_layover" android:background="#fff" android:divider="@android:color/transparent" android:scrollbars="none" /> <linearlayout android:id="@+id/bottom_layover" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:orientation="horizontal" android:weightsum="2" > <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" > </linearlayout> </linearlayout>
singlepost.xml
android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#f0f0f0" android:orientation="vertical" > <linearlayout android:id="@+id/box" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:orientation="horizontal" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingbottom="5dp" android:background="#ffffff"> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:paddingleft="5dp" android:text="brand: "> </textview> <textview android:id="@+id/brand" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#5d5d5d" android:textstyle="bold" > </textview> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:paddingleft="5dp" android:text="category: " > </textview> <textview android:id="@+id/category" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#5d5d5d" android:textstyle="bold" > </textview> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:paddingleft="5dp" android:text="description: " > </textview> <textview android:id="@+id/description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#5d5d5d" android:textstyle="bold" > </textview> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:paddingleft="5dp" android:text="code: " > </textview> <textview android:id="@+id/code" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#5d5d5d" android:textstyle="bold" > </textview> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:paddingleft="5dp" android:text="quantity: "> </textview> <textview android:id="@+id/quantity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#5d5d5d" android:textstyle="bold" > </textview> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:paddingleft="5dp" android:text="unit: " > </textview> <textview android:id="@+id/unit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#5d5d5d" android:textstyle="bold" > </textview> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:paddingleft="5dp" android:text="price: " > </textview> <textview android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#5d5d5d" android:textstyle="bold" > </textview> </linearlayout> </linearlayout> </linearlayout>
produclist.php
<?php try{ $handler= new pdo('mysql:host=localhost;dbname=account','root',''); $handler->setattribute(pdo::attr_errmode,pdo::errmode_exception); }catch(exception $e){ echo $e->getmessage(); die(); } $query =$handler->query( "select * tblproducts"); $records= array(); $records =$query->fetchall(pdo::fetch_assoc); $json["message"]=$records; echo json_encode($json); ?>
please use below code idea , customize need
class customadpter extends baseadapter implements filterable{ layoutinflater inflater = null; activity activity; itemfilter itemfilter = new itemfilter(); public customadpter(activity a) { this.activity = a; inflater = layoutinflater.from(a); } @override public int getcount() { // todo auto-generated method stub return listbillingsearch.size(); //return 3; } @override public object getitem(int position) { // todo auto-generated method stub return listbillingsearch.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view view1, viewgroup parent) { viewholder holder; if (view1 == null) { view1 = inflater.inflate(r.layout.adpter_billing, null); holder = new viewholder(); holder.txtbillinglistcompanyname = (textview) view1.findviewbyid(r.id.txtbillinglistcompanyname); holder.txtbillinglistinvoice = (textview) view1.findviewbyid(r.id.txtbillinglistinvoice); holder.txtbillingliststartdate = (textview) view1.findviewbyid(r.id.txtbillingliststartdate); holder.txtbillinglistenddate = (textview) view1.findviewbyid(r.id.txtbillinglistenddate); holder.txtbillinglistvoicecalls = (textview) view1.findviewbyid(r.id.txtbillinglistvoicecalls); holder.txtbillinglistsms = (textview) view1.findviewbyid(r.id.txtbillinglistsms); holder.txtbillinglistdataplan = (textview) view1.findviewbyid(r.id.txtbillinglistdataplan); view1.settag(holder); }else{ holder = (viewholder) view1.gettag(); } holder.txtbillinglistcompanyname.settext(listbillingsearch.get(position).getstrcompanyname()); holder.txtbillinglistinvoice.settext(listbillingsearch.get(position).getstrinvoice()); holder.txtbillingliststartdate.settext(listbillingsearch.get(position).getstrstartdate()); holder.txtbillinglistenddate.settext(listbillingsearch.get(position).getstrenddate()); holder.txtbillinglistvoicecalls.settext(listbillingsearch.get(position).getstrvoicecall()); holder.txtbillinglistsms.settext(listbillingsearch.get(position).getstrsms()); holder.txtbillinglistdataplan.settext(" "+listbillingsearch.get(position).getstrdataplan()); return view1; } @override public filter getfilter() { // todo auto-generated method stub return itemfilter; } private class itemfilter extends filter { @override protected filterresults performfiltering(charsequence constraint) { string filterstring = constraint.tostring().tolowercase(); filterresults results = new filterresults(); final list<billing> list = listbilling; int count = list.size(); final arraylist<string> nlist = new arraylist<string>(count); string filterablestring ; arraylist<billing> filterdata = new arraylist<billing>(); if(constraint.equals(null) || constraint.equals("")){ //filterdata = listbilling; results.values = listbilling; results.count = listbilling.size(); }else{ // listbillingsearch.clear(); (int = 0; < count; i++) { filterablestring = list.get(i).getstrcompanyname(); if (filterablestring.tolowercase().startswith(filterstring)) { filterdata.add(listbilling.get(i)); } } results.values = filterdata; results.count = filterdata.size(); } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { //if(results.count > 0) listbillingsearch = (arraylist<billing>) results.values; notifydatasetchanged(); } } } public static class viewholder { textview txtbillinglistcompanyname; textview txtbillinglistinvoice ; textview txtbillingliststartdate; textview txtbillinglistenddate; textview txtbillinglistvoicecalls; textview txtbillinglistsms; textview txtbillinglistdataplan; }
i hope you.
let me know if find issue.
Comments
Post a Comment