java - Simple Adapter, Invalid index when filtering -



java - Simple Adapter, Invalid index when filtering -

i'm populating listview info database, includes images text. can't filter info pass listview. have filter listview it's self. have populated listview using simple adapter , images load. problem when filtering list view crashes.(see logcat).

code i'm using:

custom simple adapter handle images

public class extendedsimpleadapter extends simpleadapter{ list<hashmap<string, string>> map; string[] from; int layout; int[] to; context context; layoutinflater minflater; public extendedsimpleadapter(context context, arraylist<hashmap<string, string>> data, int resource, string[] from, int[] to) { super(context, data, resource, from, to); layout = resource; map = data; this.from = from; this.to = to; this.context = context; } @override public view getview(int position, view convertview, viewgroup parent) { minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); homecoming this.createviewfromresource(position, convertview, parent, layout); } private view createviewfromresource(int position, view convertview, viewgroup parent, int resource) { view v; if (convertview == null) { v = minflater.inflate(resource, parent, false); } else { v = convertview; } this.bindview(position, v); homecoming v; } private void bindview(int position, view view) { final map dataset = map.get(position); if (dataset == null) { return; } final viewbinder binder = super.getviewbinder(); final int count = to.length; (int = 0; < count; i++) { final view v = view.findviewbyid(to[i]); if (v != null) { final object info = dataset.get(from[i]); string text = info == null ? "" : data.tostring(); if (text == null) { text = ""; } boolean bound = false; if (binder != null) { bound = binder.setviewvalue(v, data, text); } if (!bound) { if (v instanceof checkable) { if (data instanceof boolean) { ((checkable) v).setchecked((boolean) data); } else if (v instanceof textview) { // note: maintain instanceof textview check @ bottom of these // ifs since lot of views textviews (e.g. checkboxes). setviewtext((textview) v, text); } else { throw new illegalstateexception(v.getclass().getname() + " should bound boolean, not " + (data == null ? "<unknown type>" : data.getclass())); } } else if (v instanceof textview) { // note: maintain instanceof textview check @ bottom of these // ifs since lot of views textviews (e.g. checkboxes). setviewtext((textview) v, text); } else if (v instanceof imageview) { if (data instanceof integer) { setviewimage((imageview) v, (integer) data); } else if (data instanceof string) { setviewimage((imageview) v, (string) data); } else { setviewimage((imageview) v, text); } } else { throw new illegalstateexception(v.getclass().getname() + " not " + " view can bounds simpleadapter"); } } } } } @override public int getcount() { homecoming super.getcount(); } public void setviewimage(imageview v, string bmp) { v.setimagebitmap(imagetools.decodebase64(bmp)); } }

calling class:

adapter = new extendedsimpleadapter( getactivity(), doctorslist, r.layout.listview_item_layout_doctor, new string[]{key_id, key_name, key_specialty, key_rating, key_account, key_active, key_image}, new int[]{r.id.id, r.id.fname, r.id.specialty, r.id.rating, r.id.account, r.id.active, r.id.doctorimage});

logcat:

java.lang.indexoutofboundsexception: invalid index 2, size 2 @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255) @ java.util.arraylist.get(arraylist.java:308) @ my.app.adapters.extendedsimpleadapter.bindview(extendedsimpleadapter.java:61) @ my.app.adapters.extendedsimpleadapter.createviewfromresource(extendedsimpleadapter.java:54) @ my.app.adapters.extendedsimpleadapter.getview(extendedsimpleadapter.java:41)

i fixed implementing custom filter:

private class customfilter extends filter { @override protected filterresults performfiltering(charsequence prefix) { filterresults results = new filterresults(); if (munfiltereddata == null) { munfiltereddata = new arraylist<hashmap<string, string>>(map); } if (prefix == null || prefix.length() == 0) { list<hashmap<string, string>> list = munfiltereddata; results.values = list; results.count = list.size(); } else { string prefixstring = prefix.tostring().tolowercase(); list<hashmap<string, string>> unfilteredvalues = munfiltereddata; int count = unfilteredvalues.size(); arraylist<map<string, ?>> newvalues = new arraylist<map<string, ?>>(count); (int = 0; < count; i++) { map<string, ?> h = unfilteredvalues.get(i); if (h != null) { int len = to.length; (int j=0; j<len; j++) { string str = (string)h.get(from[j]); string[] words = str.split(" "); int wordcount = words.length; (int k = 0; k < wordcount; k++) { string word = words[k]; if (word.tolowercase().contains(prefixstring)) { newvalues.add(h); break; } } } } } results.values = newvalues; results.count = newvalues.size(); } homecoming results; } @suppresswarnings("unchecked") protected void publishresults(charsequence constraint, filterresults results) { //remove duplicates map.addall((java.util.collection<? extends hashmap<string, string>>) results.values); hashset hs = new hashset(); hs.addall(map); map.clear(); map.addall(hs); if (results.count > 0) { notifydatasetchanged(); } else { notifydatasetinvalidated(); } } }

add adapter:

@override public filter getfilter() { if(filter != null) homecoming filter; else homecoming filter = new customfilter(); }

java android listview android-listview hashmap

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -