android - Creating SearchBox in listView -


i have created "search" box in android. when try search not showing updated value in list. value coming in list search related value not changing. here snapshot:

this search box image

if enter "ab" it's working fine, when putting "s", it's showing first listview item this:

image 2

here fragment class using addtextchangelistener:

etsearchuser.addtextchangedlistener(new textwatcher() {         @override         public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) {          }          @override         public void ontextchanged(charsequence charsequence, int i, int i1, int i2) {             if (i2<i1){                 adapter.resetdata();             }             adapter.getfilter().filter(charsequence);         }          @override         public void aftertextchanged(editable editable) {          }     }); 

here adapter class:

public class removeuseradapter extends baseadapter implements filterable { private arraylist<usermodel> listcontent; private arraylist<usermodel> searchlist; context context; private filter planetfilter; usermodel content, content1; layoutinflater inflater;  public removeuseradapter(context context, arraylist listcontent) {     this.context = context;     this.listcontent = listcontent;     this.searchlist = listcontent;     inflater = layoutinflater.from(context); }  @override public int getcount() {     return listcontent.size(); }  @override public object getitem(int i) {      return listcontent.get(i); }  @override public long getitemid(int i) {     return listcontent.get(i).hashcode(); }   class viewholder {     private textview tvemail, tvuserid;     private checkbox checkbox; }  @override public view getview(int i, view view, viewgroup viewgroup) {     final viewholder viewholder, viewholder1;     if (view == null) {         layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);         view = inflater.inflate(r.layout.remove_user_adapter, null);         viewholder = new viewholder();         viewholder.tvemail = (textview) view.findviewbyid(r.id.tvemailuser);         viewholder.tvuserid = (textview) view.findviewbyid(r.id.tvusergroup);         viewholder.checkbox = (checkbox) view.findviewbyid(r.id.checkbox);         viewholder.checkbox.setvisibility(view.invisible);         view.settag(viewholder);      } else {         viewholder = (viewholder) view.gettag();     }       content = (usermodel) listcontent.get(i);     content = (usermodel) searchlist.get(i);     //log.i("@@@",content.getemail()+" email");     viewholder.tvemail.settext(content.getemail());     viewholder.tvuserid.settext(content.getusergroup());     return view; }  public void resetdata() {     listcontent = searchlist; }  @override public filter getfilter() {     filter filter = new filter() {          @override         protected void publishresults(charsequence charsequence, filterresults result) {             listcontent = (arraylist<usermodel>) result.values;             notifydatasetchanged();         }          @override         protected filterresults performfiltering(charsequence constraint) {             filterresults results = new filterresults();             arraylist<usermodel> filteredarrlist = new arraylist<usermodel>();             if (searchlist == null) {                 searchlist = new arraylist<usermodel>(listcontent);             }             if (constraint == null || constraint.length() == 0) {                  // set original result return                 results.count = searchlist.size();                 results.values = searchlist;             } else {                 constraint = constraint.tostring().tolowercase();                 (int = 0; < searchlist.size(); i++) {                     content = (usermodel) searchlist.get(i);                     string data = content.getemail();                     string user = content.getuserid();                     if (data.tolowercase().startswith(constraint.tostring())) {                         filteredarrlist.add(new usermodel(data, user));                     }                 }                 results.count = filteredarrlist.size();                 results.values = filteredarrlist;             }             return results;         }      };     return filter; } 

}

for search using simple edittext.

and 1 more thing it's searching first letter of gmail. want make search randomly.

for example: abc@gmail.com there, if enter in search box "gmail" should show result.

here usermodel class:

public class usermodel { private string usergroup; private string name; private string email; private string phone; private string password; private string userid;  public usermodel(string email, string usergroup) {     super();     this.email = email;     this.usergroup = usergroup; } public usermodel(){}  public string getuserid() {     return userid; }  public void setuserid(string userid) {     this.userid = userid; }  public string getpassword() {     return password; }  public void setpassword(string password) {     this.password = password; }  public string getusergroup() {     return usergroup; }  public void setusergroup(string usergroup) {     this.usergroup = usergroup; }  public string getphone() {     return phone; }  public void setphone(string phone) {     this.phone = phone; }  public string getemail() {     return email; }  public void setemail(string email) {     this.email = email; }  public string getname() {     return name; }  public void setname(string name) {     this.name = name; } 

}

in getcount must return filtered list count not original list count. see gist more information: https://gist.github.com/fjfish/3024308

i update code:

public class removeuseradapter extends baseadapter implements filterable { private arraylist<usermodel> listcontent; private arraylist<usermodel> searchlist; context context; private filter planetfilter; usermodel content, content1; layoutinflater inflater;  public removeuseradapter(context context, arraylist listcontent) {     this.context = context;     this.listcontent = listcontent;     this.searchlist = listcontent;     inflater = layoutinflater.from(context); }  @override public int getcount() {     return searchlist.size(); }  @override public object getitem(int i) {      return searchlist.get(i); }  @override public long getitemid(int i) {     return searchlist.get(i).hashcode(); }   class viewholder {     private textview tvemail, tvuserid;     private checkbox checkbox; }  @override public view getview(int i, view view, viewgroup viewgroup) {     final viewholder viewholder, viewholder1;     if (view == null) {         layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);         view = inflater.inflate(r.layout.remove_user_adapter, null);         viewholder = new viewholder();         viewholder.tvemail = (textview) view.findviewbyid(r.id.tvemailuser);         viewholder.tvuserid = (textview) view.findviewbyid(r.id.tvusergroup);         viewholder.checkbox = (checkbox) view.findviewbyid(r.id.checkbox);         viewholder.checkbox.setvisibility(view.invisible);         view.settag(viewholder);      } else {         viewholder = (viewholder) view.gettag();     }       content = (usermodel) getitem(i);     viewholder.tvemail.settext(content.getemail());     viewholder.tvuserid.settext(content.getusergroup());     return view; }  public void resetdata() {     searchlist=listcontent; }  @override public filter getfilter() {     filter filter = new filter() {          @override         protected void publishresults(charsequence charsequence, filterresults result) {             searchlist = (arraylist<usermodel>) result.values;             notifydatasetchanged();         }          @override         protected filterresults performfiltering(charsequence constraint) {             filterresults results = new filterresults();             arraylist<usermodel> filteredarrlist = new arraylist<usermodel>();              if (constraint == null || constraint.length() == 0) {                  // set original result return                 results.count = listcontent.size();                 results.values = listcontent;             } else {                 constraint = constraint.tostring().tolowercase();                 (int = 0; < listcontent.size(); i++) {                     content = (usermodel) listcontent.get(i);                     string data = content.getemail();                     string user = content.getuserid();                     if (data.tolowercase().startswith(constraint.tostring())) {                         filteredarrlist.add(content);                     }                 }                 results.count = filteredarrlist.size();                 results.values = filteredarrlist;             }             return results;         }      };     return filter; } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -