Android JSON listview search function(update) -


1.my search function works filter int (ex:rank) if want filter char(ex:country name),how should change code?

2.when type in medittext delete it,the listview become empty,how see json listview again if medittext empty ?

for filter works fine,i can't figure out how solve these 2 question.thanks

import android.app.activity; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.text.editable; import android.text.textwatcher; import android.util.log; import android.widget.edittext; import android.widget.listview;  import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import java.util.arraylist; import java.util.hashmap;  public class mainactivity extends activity {     // declare variables     jsonobject jsonobject;     jsonarray jsonarray;     listview listview;     listviewadapter adapter;     progressdialog mprogressdialog;     arraylist<hashmap<string, string>> arraylist;     static string rank = "rank";     static string country = "country";     static string population = "population";     static string url="url";     static string flag = "flag";     edittext medittext;     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         // view listview_main.xml         setcontentview(r.layout.listview_main);         // execute downloadjson asynctask         new downloadjson().execute();             medittext = (edittext) findviewbyid(r.id.inputsearch);         medittext.addtextchangedlistener(new textwatcher() {              public void aftertextchanged(editable s) {              }              public void beforetextchanged(charsequence s, int start, int count, int after) {              }              public void ontextchanged(charsequence s, int start, int before, int count) {                      arraylist<hashmap<string, string>> arraytemplist = new arraylist<hashmap<string, string>>();                     string searchstring = medittext.gettext().tostring();                     (int = 0; < arraylist.size(); i++) {                         string currentstring = arraylist.get(i).get(mainactivity.rank);                         if (searchstring.equalsignorecase(currentstring)) {                             arraytemplist.add(arraylist.get(i));                         }                      }                     adapter = new listviewadapter(mainactivity.this, arraytemplist);                     listview.setadapter(adapter);              }           });     }       // downloadjson asynctask     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("android json parse tutorial");             // set progressdialog message             mprogressdialog.setmessage("loading...");             mprogressdialog.setindeterminate(false);             // show progressdialog             mprogressdialog.show();         }          @override         protected void doinbackground(void... params) {             // create array             arraylist = new arraylist<hashmap<string, string>>();             // retrieve json objects given url address             jsonobject = jsonfunctions                     .getjsonfromurl("http://ndublog.twomini.com/123.txt.txt");              try {                 // locate array name in json                 jsonarray = jsonobject.getjsonarray("worldpopulation");                  (int = 0; < jsonarray.length(); i++) {                     hashmap<string, string> map = new hashmap<string, string>();                     jsonobject = jsonarray.getjsonobject(i);                     // retrive json objects                     map.put("rank", jsonobject.getstring("rank"));                     map.put("country", jsonobject.getstring("country"));                     map.put("population", jsonobject.getstring("population"));                     map.put("url",jsonobject.getstring("url"));                     map.put("flag", jsonobject.getstring("flag"));                     // set json objects array                     arraylist.add(map);                 }             } catch (jsonexception e) {                 log.e("error", e.getmessage());                 e.printstacktrace();             }             return null;         }          @override         protected void onpostexecute(void args) {             // locate listview in listview_main.xml             listview = (listview) findviewbyid(r.id.listview);             // pass results listviewadapter.java             adapter = new listviewadapter(mainactivity.this, arraylist);             // set adapter listview             listview.setadapter(adapter);             // close progressdialog             mprogressdialog.dismiss();         }     }            } 

change adapter adapter01 (the adapter01 1 initialized)

        @override         public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) {             // when user changed text             mainactivity.this.adapter01.getfilter().filter(cs);         } 

for open site on listview, create itemclicklistener:

listview.setonitemclicklistener(new onitemclicklistener() {     public void onitemclick(adapterview<?> parent, view view, int position, long id) {         string url = null;        (string s : adapter01.getitems()) {            if (s.hashcode() == id) {               url = s;            }        }         intent = new intent(intent.action_view);        i.setdata(uri.parse(url));        mainactivity.this.startactivity(i);      } });  

and override getitemid() adapter.

example:

 protected long getitemid(int position) {      return getitem(position).hashcode();  } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -