android - ListView Not Updating After Filtering, when the backspace key is entered -
here code, used custom adapter, , applied filter method, listview not update when backspace key entered, on when 1 switches item activity. please me.. , how display no result filtered list..
public class itemlistadapter extends arrayadapter<itemvo> { private arraylist<itemvo> itemlist; private arraylist<itemvo> fiems; private static final int long_delay = 3500; // 3.5 seconds private static final int short_delay = 2000; // 2 seconds private context context; private filter filter; public itemlistadapter(context context, int textviewresourceid,arraylist<itemvo> statelist) { super(context, textviewresourceid, statelist); this.context = context; this.itemlist = new arraylist<itemvo>(); this.itemlist.addall(statelist); this.fiems=new arraylist<itemvo>(); this.fiems.addall(statelist); } private class viewholder { textview code; checkbox name; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; log.v("convertview", string.valueof(position)); if (convertview == null) { layoutinflater vi = (layoutinflater)context.getsystemservice(context.layout_inflater_service); convertview = vi.inflate(r.layout.listview_items, null); holder = new viewholder(); holder.code = (textview) convertview.findviewbyid(r.id.textview1); holder.name = (checkbox) convertview.findviewbyid(r.id.checkbox1); convertview.settag(holder); holder.name.setonclicklistener( new view.onclicklistener() { public void onclick(view v) { checkbox cb = (checkbox) v; itemvo item = (itemvo) cb.gettag(); item.setselected(cb.ischecked()); } }); } else { holder = (viewholder) convertview.gettag(); } itemvo state = itemlist.get(position); holder.code.settext(state.getitemdescription()); holder.name.setchecked(state.isselected()); holder.name.settag(state); return convertview; } @override public filter getfilter() { if (filter == null) filter = new pkmnnamefilter(); return filter; } private class pkmnnamefilter extends filter { @suppresswarnings("unused") @suppresslint("defaultlocale") @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); // holds results of filtering operation in values string prefix = constraint.tostring().tolowercase(); if (prefix == null || prefix.length() == 0) { arraylist<itemvo> list = new arraylist<itemvo>(fiems); results.values = list; results.count = list.size(); } else { final arraylist<itemvo> list = new arraylist<itemvo>(fiems); final arraylist<itemvo> nlist = new arraylist<itemvo>(); int count = list.size(); (int i=0; i<count; i++) { final itemvo pkmn = list.get(i); final string value = pkmn.getitemdescription().tolowercase(); if (value.startswith(prefix)) { nlist.add(pkmn); } /*else { final toast toast= toast.maketext(context, "no items found", toast.length_short); toast.show(); handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { toast.cancel(); } }, 1000); }*/ } // set filtered result return results.values = nlist; results.count = nlist.size(); } return results; } @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint, filterresults results) { itemlist = (arraylist<itemvo>)results.values; // has filtered values clear(); int count = itemlist.size(); (int i=0; i<count; i++) { itemvo pkmn = (itemvo)itemlist.get(i); add(pkmn); notifydatasetchanged(); // notifies data new filtered values } } } public void notifydatasetchanged() { super.notifydatasetchanged(); boolean notifychanged = true; } }
in main activity for editetxt..
dataadapter = new itemlistadapter(this,r.layout.listview_items, itemlist); lv.setadapter(dataadapter); final edittext searchbox=(edittext) findviewbyid(r.id.filter_text); //searchbox.addtextchangedlistener(this); lv.settextfilterenabled(true); // add text change listener edittext searchbox.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // call adapter current character filter log.d("constants.tag", "*** search value changed: " + cs.tostring()); cateringlist.this.dataadapter.getfilter().filter(cs); dataadapter.notifydatasetchanged(); } @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { } @override public void aftertextchanged(editable arg0) { //if(arg0.length()==0) // { // } } });
you need validation if arraylist have data put in getview() method
if (itemlist == null || itemlist.size() == 0){ //do code here holder.code.settext("no result"); }
brother change line:
clear(); int count = itemlist.size(); (int i=0; i<count; i++) { itemvo pkmn = (itemvo)itemlist.get(i); add(pkmn); notifydatasetchanged(); }
to this:
itemlist = (arraylist<itemvo>)results.values; notifydatasetchanged();
try one:
private arraylist<itemvo> mitemlist; public filter getfilter() { return new filter(){ @override protected filterresults performfiltering(charsequence constraint) { final filterresults oresults = new filterresults(); final arraylist<itemvo> result = new arraylist<itemvo>(); if (mitemlist == null) mitemlist = itemlist; if (constraint != null){ if (mitemlist != null && mitemlist.size > 0){ //loop (final itemvo item : mitemlist){ final string value = item.getitemdescription().tolowercase(); if (value.startswith(constraint.tostring())){ result.add(item); } } } } } oresults.values = result; } @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint,filterresults results) { itemlist = (arraylist<itemvo>)results.values; notifydatasetchanged(); } }
Comments
Post a Comment