listview - Search Bar for an ExpandableListView -
in code use custom expandablelistviewadapter use baseexpandablelistadapter. trying filter parent groups when searches specific word in search bar. trying use itextwatcher however, when use .filter expandablelistviewadapter madapter code wont build. can't use invokefilter(s) filter out parents. can please help? thank in advance!
again trying filter through parents; not children. main:
using android.app; using android.widget; using android.os; using system.net; using java.lang; using system; using newtonsoft.json.linq; using system.linq; using java.util; using system.threading; using org.json; using android.content; using android.views; using system.collections.generic; using system.text; using restsharp.extensions.monohttp; using android.text; namespace dictionarye { [activity(label = "dictionarye", mainlauncher = true, icon = "@drawable/logo")] public class mainactivity : activity { private expandablelistview list; private expandablelistviewadapter madapter; private arrayadapter<string> adapter; private int length; private list<string> group= new list<string>(); private string[] names; private dictionary<string, list<string>> mapout = new dictionary<string, list<string>>(); private searchview searchbar; private view.ionclicklistener listener; protected override void oncreate(bundle bundle) { base.oncreate(bundle); setcontentview(resource.layout.main); actionbar.hide(); // set views searchbar = findviewbyid<searchview>(resource.id.searchbar); list = findviewbyid<expandablelistview>(resource.id.lv); //set groups webclient client = new webclient(); string json = client.downloadstring("********************"); jsonarray myarray = new jsonarray(json); length = myarray.length(); names = new string[length]; (int = 0; < length; i++) { jsonobject element = myarray.getjsonobject(i); names[i] = element.getstring("name"); } setdata(out madapter); list.setadapter(madapter); madapter.filter.invokefilter(); } private void setdata(out expandablelistviewadapter madapter) { string urlholder; string url; string json; string time; string timestamp; string together; webclient client1 = new webclient(); (int i=0;i < length; i++) { list<string> listplaceholder = new list<string>(); group.add(names[i]); urlholder = uri.escapedatastring(names[i]); url = "**********"; json = client1.downloadstring(url); jsonarray array2 = new jsonarray(json); int length2 = array2.length(); (int j = 0; j < length2; j++) { jsonobject element = array2.getjsonobject(j); time=element.getstring("wait"); jsonobject timeelement = array2.getjsonobject(j); timestamp = timeelement.getstring("created_at"); timestamp=timestamp.replace("t", " @ "); int index = timestamp.indexof("."); if (index > 0) { timestamp = timestamp.substring(0, index); } = time + " minutes posted @ " + timestamp; listplaceholder.add(together); } mapout.add(group[i], listplaceholder); } madapter = new expandablelistviewadapter(this, group, mapout); } } }
this custom expandable base adapter:
using system; using system.collections.generic; using system.linq; using system.text; using android.app; using android.content; using android.os; using android.runtime; using android.views; using android.widget; using java.lang; namespace dictionarye { public class expandablelistviewadapter : baseexpandablelistadapter { private context context; private list<string> listgroup; private dictionary<string, list<string>> listchild; public expandablelistviewadapter(context context, list<string> listgroup, dictionary<string, list<string>> listchild) { this.context = context; this.listgroup = listgroup; this.listchild = listchild; } public override int groupcount { { return listgroup.count; } } public override bool hasstableids { { return false; } } public override java.lang.object getchild(int groupposition, int childposition) { var result = new list<string>(); listchild.trygetvalue(listgroup[groupposition], out result); return result[childposition]; } public override long getchildid(int groupposition, int childposition) { return childposition; } public override int getchildrencount(int groupposition) { var result = new list<string>(); listchild.trygetvalue(listgroup[groupposition], out result); return result.count; } public override view getchildview(int groupposition, int childposition, bool islastchild, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater inflater = (layoutinflater)context.getsystemservice(context.layoutinflaterservice); convertview = inflater.inflate(resource.layout.children, null); } textview textviewitem = convertview.findviewbyid<textview>(resource.id.datavalue); string content = (string)getchild(groupposition, childposition); textviewitem.text = content; return convertview; } public override java.lang.object getgroup(int groupposition) { return listgroup[groupposition]; } public override long getgroupid(int groupposition) { return groupposition; } public override view getgroupview(int groupposition, bool isexpanded, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater inflater = (layoutinflater)context.getsystemservice(context.layoutinflaterservice); convertview = inflater.inflate(resource.layout.groups, null); } string textgroup = (string)getgroup(groupposition); textview textviewgroup = convertview.findviewbyid<textview>(resource.id.header); textviewgroup.text = textgroup; return convertview; } public override bool ischildselectable(int groupposition, int childposition) { return true; } }
}
filter method not valid in given context.
to use filter on adapter, need let adapter implements ifilterable
interface:
public class expandablelistviewadapter : baseexpandablelistadapter,ifilterable { ... }
update:
if have implemented interface, default filter
empty below:
public class expandablelistviewadapter : baseexpandablelistadapter,ifilterable { ... filter=>throw new notimplementedexception(); }
that's why getting exception. needs done create custom filter class:
public class groupfilter:filter { expandablelistviewadapter _adapter; public groupfilter(expandablelistviewadapter adapter) { _adapter = adapter; } protected override filterresults performfiltering(icharsequence constraint) { var result = new filterresults(); // add filtered items filterresults //convert net object java object return result; } protected override void publishresults(icharsequence constraint, filterresults results) { //convert java object net object //call _adapter.notifydatasetchanged(); } }
and initialize filter in adapter:
public class expandablelistviewadapter : baseexpandablelistadapter,ifilterable { ... public filter filter => new groupfilter(this); }
for complete implementation sample of filer, can refer cheesebaron's demo here.
Comments
Post a Comment