java - Android ListView Search Adapter Issues -


i have problem implementing search feature listview.

basically, data remote server put in list. think maybe related arrayadapter because list item supposed have id , name rather 1 parameter.

here relevant files involved: itemlistactivity.java

public class itemlistactivity extends listactivity {  // progress dialog private progressdialog pdialog;  // creating json parser object jsonparser jparser = new jsonparser();  arraylist<hashmap<string, string>> itemlist; list<string> items_search = new arraylist<string>();  listview lv; arrayadapter<string> adapter;  // url products list private static string url_all_items = "http://199.0.0.103/pawnshop_2/index.php/android/get_item_list";  // json node names private static final string tag_success = "success"; private static final string tag_items = "items"; private static final string tag_iid = "i_id"; private static final string tag_name = "name";  // products jsonarray jsonarray items = null;  //int cid;    textview tvnoitems,tvdebug; edittext etsearchitem; //listview searched;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_item_list);      tvnoitems = (textview) findviewbyid(r.id.tvnoitems);     etsearchitem = (edittext) findviewbyid(r.id.etsearchitem);     lv = (listview) findviewbyid(android.r.id.list);     tvdebug = (textview) findviewbyid(r.id.tvdebug);     bundle extras = getintent().getextras();       // hashmap listview     itemlist = new arraylist<hashmap<string, string>>();      //adapter = new arrayadapter<string>(this, r.layout.)     // loading products in background thread     new loadallitems().execute();      // listview     //lv = getlistview();     //final int i=0;      string products[] = {"diamond , emerald necklace","diamond drop earrings","new item","pokemon x","pokemon y","heliptile"};      adapter = new arrayadapter<string>(this, r.layout.activity_item_detail, r.id.name, products);      // updating listview     //setlistadapter(adapter);     lv.setadapter(adapter);      // on selecting single product     // launching edit product screen     /*     lv.setonitemclicklistener(new onitemclicklistener() {          @override         public void onitemclick(adapterview<?> parent, view view,                 int position, long id) {             // getting values selected listitem             string i_id = ((textview) view.findviewbyid(r.id.i_id)).gettext()                     .tostring();              // starting new intent             intent in = new intent(getapplicationcontext(),                     singleitemactivity.class);             // sending pid next activity             in.putextra(tag_iid, i_id);              // starting new activity , expecting response             startactivity(in);         }     });*/      etsearchitem.addtextchangedlistener(new textwatcher() {          @override         public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) {             // when user changed text            itemlistactivity.this.adapter.getfilter().filter(cs);              string items= "";            for(int i=0; i<items_search.size();i++){                items+=" "+items_search.get(i);            }            tvdebug.settext("search: "+ items);         }          @override         public void beforetextchanged(charsequence arg0, int arg1, int arg2,                 int arg3) {             // todo auto-generated method stub          }          @override         public void aftertextchanged(editable arg0) {             // todo auto-generated method stub          }     });  }  /**  * background async task load product making http request  * */ class loadallitems extends asynctask<string, string, string> {     int success = 0;     /**      * before starting background thread show progress dialog      * */     @override     protected void onpreexecute() {         super.onpreexecute();         pdialog = new progressdialog(itemlistactivity.this);         pdialog.setmessage("loading items. please wait...");         pdialog.setindeterminate(false);         pdialog.setcancelable(false);         pdialog.show();     }      /**      * getting products url      * */     protected string doinbackground(string... args) {         // building parameters         list<namevaluepair> params = new arraylist<namevaluepair>();         //params.add(new basicnamevaluepair("c_id",cid+""));         // getting json string url         jsonobject json = jparser.makehttprequest(url_all_items, "post", params);          log.d("items: ", json.tostring());          try {             // checking success tag             success = json.getint(tag_success);              if (success == 1) {                 // transactions found                 // getting array of products                 items = json.getjsonarray(tag_items);                  // looping through products                 (int = 0; < items.length(); i++) {                     jsonobject c = items.getjsonobject(i);                      // storing each json item in variable                     string i_id = c.getstring(tag_iid);                     string name= c.getstring(tag_name);                      items_search.add(name);                      // creating new hashmap                     hashmap<string, string> map = new hashmap<string, string>();                      // adding each child node hashmap key => value                     map.put(tag_iid, i_id);                     map.put(tag_name, name);                      // adding hashlist arraylist                     itemlist.add(map);                 }             }          } catch (jsonexception e) {             e.printstacktrace();         }          return null;     }      /**      * after completing background task dismiss progress dialog      * **/     protected void onpostexecute(string file_url) {         // dismiss dialog after getting products         pdialog.dismiss();          if (success==0){             tvnoitems.settext("no items found.");         }          // updating ui background thread         runonuithread(new runnable() {             public void run() {                 /**                  * updating parsed json data listview                  * */                 listadapter adapter = new simpleadapter(                         itemlistactivity.this, itemlist,                         r.layout.activity_item_detail, new string[] { tag_iid,                                 tag_name},                         new int[] { r.id.i_id, r.id.name });                 // updating listview                 setlistadapter(adapter);             }         });      }  } 

here example custom adapter

using lists in android (listview) - tutorial

this example search data.

link


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -