java - on listview filtration is not working properly -
i facing problem while filtration of list .i using ontextchange() method .it working problem type single character in edit text soft keyboard gets disappear , have tap edit text again type complete string .i have done code soft keyboard not disappear after text change type still have tap on edit text.here code :
search.addtextchangedlistener(new textwatcher() { @ override public void ontextchanged(charsequence s, int start, int before, int count) { // todo auto-generated method stub string startwith = s.tostring(); arraylist < myshares > filterlist = new arraylist < myshares > (); (int = 0; < filelist.size(); i++) { if (filelist.get(i).getname().tolowercase().startswith(startwith) || filelist.get(i).getname().touppercase().startswith(startwith)) { filterlist.add(filelist.get(i)); } } adapter = new mlistadapter(playlistactivity.this, filterlist); playlistview.setadapter(adapter); adapter.notifydatasetchanged(); } @ override public void beforetextchanged(charsequence s, int start, int count, int after) { } @ override public void aftertextchanged(editable s) { search.setselection(search.gettext().length()); inputmethodmanager imm = (inputmethodmanager) getsystemservice(context.input_method_service); imm.showsoftinput(search, inputmethodmanager.show_implicit); } });
here xml code edit text:
<edittext android:ems="10" android:id="@+id/search_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:layout_weight="2" android:background="@drawable/searchbox" android:editable="true" android:focusable="true" android:inputtype="textfilter" android:hint="search" android:keepscreenon="true" android:paddingleft="10dp" android:textsize="15dp" />
i have set android:focusable="false " property on list view doesn't overlap soft keyboard. problem edit text still same .here screenshots
as insert single character list gets filter , cursor gone edit text box.
i went through arrayadapter's actual source code , looks written behave way.
arrayadapter has 2 lists begin with: mobjects , moriginalvalues. mobjects primary data set adapter work with. taking add() function, example:
public void add(t object) { if (moriginalvalues != null) { synchronized (mlock) { moriginalvalues.add(object); if (mnotifyonchange) notifydatasetchanged(); } } else { mobjects.add(object); if (mnotifyonchange) notifydatasetchanged(); }
}
moriginalvalues null operations (add, insert, remove, clear) default targeted mobjects. fine until moment decide enable filtering on list , perform one. filtering first time initializes moriginalvalues whatever mobjects has:
private class arrayfilter extends filter { @override protected filterresults performfiltering(charsequence prefix) { filterresults results = new filterresults(); if (moriginalvalues == null) { synchronized (mlock) { moriginalvalues = new arraylist<t>(mobjects); //moriginalvalues no longer null } } if (prefix == null || prefix.length() == 0) { synchronized (mlock) { arraylist<t> list = new arraylist<t>(moriginalvalues); results.values = list; results.count = list.size(); } } else { //filtering work happens here , new filtered set stored in newvalues results.values = newvalues; results.count = newvalues.size(); } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { //noinspection unchecked mobjects = (list<t>) results.values; if (results.count > 0) { notifydatasetchanged(); } else { notifydatasetinvalidated(); } } }
moriginalvalues has copy of, well, original values/items, adapter work , display filtered list thru mobjects without losing pre-filtered data.
now forgive me (and please tell , explain) if thinking incorrect find weird because moriginalvalues no longer null, subsequent calls of adapter operations modify moriginalvalues. since adapter set @ mobjects primary data set, appear on screen nothing happening. until perform round of filtering. removing filter triggers this:
if (prefix == null || prefix.length() == 0) { synchronized (mlock) { arraylist<t> list = new arraylist<t>(moriginalvalues); results.values = list; results.count = list.size(); } }
moriginalvalues, we've been modifying since our first filter (although couldn't see happening on screen) stored in list , copied on mobjects, displaying changes made. nevertheless point onwards: operations done on moriginalvalues, , changes appear after filtering.
as solution, i've come @ moment either (1) put boolean flag tells adapter operations if there ongoing filtering or not--if filtering complete, copy on contents of moriginalvalues mobjects, or (2) call adapter's filter object , pass empty string *.getfilter().filter("") force filter after every operation [as suggested santhosh].
it highly appreciated if can shed more light on issue or confirm did. thank you!
Comments
Post a Comment