android - ListView Search is not Working Porperly -
hello friends have started learning android days working on list views want search items in list view. gi have gone through many post , wrote following code how not working . please .
qyestionactivity
public class questionactivity extends actionbaractivity { string filepath = null; string question_filepath = null; controller c; listview question_list_view; edittext txt_search; arraylist<singlequestion> question_list; main_questionlist_adapter questionlistadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_question); filepath = constant.application_file_path; question_filepath = filepath + "questions.txt"; file test_fifile = new file(question_filepath); if (!test_fifile.exists()) { copyassets(); } new parsequestionfile().parsequestionfile(question_filepath); c = controller.getcontroller(); question_list = c.getquestions().getquestions(); question_list_view = (listview) findviewbyid(r.id.lst_questionlit); txt_search = (edittext) findviewbyid(r.id.edittext1); questionlistadapter = new main_questionlist_adapter( questionactivity.this); question_list_view.setadapter(questionlistadapter); question_list_view.settextfilterenabled(true); } protected void onresume() { super.onresume(); txt_search.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { questionlistadapter.getfilter().filter(s); questionlistadapter.notifydatasetinvalidated(); questionlistadapter.notifydatasetchanged(); question_list_view.setadapter(questionlistadapter); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } }); }
adapter class
public class main_questionlist_adapter extends baseadapter implements filterable {
controller c = controller.getcontroller(); arraylist<singlequestion> questions_aerraylist; layoutinflater inflater; context context; singlequestion singlequestion; filter filter; public main_questionlist_adapter(context context) { this.context = context; questions_aerraylist = new arraylist<singlequestion>(); questions_aerraylist = c.getquestions().getquestions(); inflater = layoutinflater.from(context); } @override public int getcount() { return questions_aerraylist.size(); } @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return position; } @override public view getview(final int position, view currentview, viewgroup parentview) { questioviewholder questionviewholder; if (currentview == null) { currentview = inflater .inflate( com.example.ten_step.r.layout.main_question_list_adapter_view, null); questionviewholder = new questioviewholder(); currentview.settag(questionviewholder); } else { questionviewholder = (questioviewholder) currentview.gettag(); } questionviewholder.question_text_view = (textview) currentview .findviewbyid(com.example.ten_step.r.id.txt_question_text); singlequestion = new singlequestion(); singlequestion = questions_aerraylist.get(position); questionviewholder.question_text_view.settext(singlequestion .getquestion().tostring()); questionviewholder.question_text_view.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(singlequestion != null) { c.setselected_question(questions_aerraylist.get(position)); toast.maketext(context, c.getselected_question().getquestion(), toast.length_long).show(); } else { toast.maketext(context, "please tr againg", toast.length_long).show(); } } }); return currentview; } class questioviewholder { textview question_text_view; } @override public filter getfilter() { if (filter == null) { filter = new questionfilter(); } return filter; } private class questionfilter extends filter { arraylist<singlequestion> actual_list; arraylist<singlequestion> sorted_list; controller controller; public questionfilter() { actual_list = new arraylist<singlequestion>(); controller = controller.getcontroller(); actual_list = controller.getquestions().getquestions(); sorted_list = new arraylist<singlequestion>(); } @override protected filterresults performfiltering(charsequence searchquer) { string search_query = searchquer.tostring().tolowercase(); filterresults result = new filterresults(); if(search_query != null || search_query.length() !=0 ) { (int = 0 ; < actual_list.size() ; i++) { if(actual_list.get(i).getquestion().tolowercase().contains(search_query) && !sorted_list.contains(actual_list.get(i)) ) { sorted_list.add(actual_list.get(i)); } } if(sorted_list.size() !=0) { result.values = sorted_list; result.count = sorted_list.size(); } } else { result.values = actual_list; result.count = actual_list.size(); } c.getquestions().setquestion((arraylist<singlequestion>) result.values); return result; } @override protected void publishresults(charsequence searchquer, filterresults results) { arraylist<singlequestion> new_sorted_list = (arraylist<singlequestion>) results.values; if (results.count > 0) { c.getquestions().setquestion(new_sorted_list); // notifydatasetchanged(); } else { notifydatasetinvalidated(); } } }
}
thanks in advance.
i have done search in list view in bit different manner. have used textwatcher instead of implementing filterable interface adapter.
please take @ following code , provide suggestion if have.
public class questionactivity extends actionbaractivity { string filepath = null; string question_filepath = null; controller c; listview question_list_view; edittext txt_search; arraylist<singlequestion> question_list; main_questionlist_adapter questionlistadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_question); filepath = constant.application_file_path; question_filepath = filepath + "questions.txt"; file test_fifile = new file(question_filepath); if (!test_fifile.exists()) { copyassets(); } new parsequestionfile().parsequestionfile(question_filepath); c = controller.getcontroller(); question_list = c.getquestions().getquestions(); question_list_view = (listview) findviewbyid(r.id.lst_questionlit); txt_search = (edittext) findviewbyid(r.id.edittext1); questionlistadapter = new main_questionlist_adapter( questionactivity.this , c.getquestions().getquestions()); question_list_view.setadapter(questionlistadapter); question_list_view.settextfilterenabled(true); txt_search.addtextchangedlistener(searchtextchangelistener); } protected void onresume() { super.onresume(); } textwatcher searchtextchangelistener = new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { arraylist<singlequestion> defaultquestinlist = c.getquestions() .getquestions(); if (s.length() > 0) { set<singlequestion> sortedquestionset = new hashset<singlequestion>(); int questionlistcount = defaultquestinlist.size(); string searchquery = s.tostring().tolowercase(); (int = 0; < questionlistcount; i++) { if (defaultquestinlist.get(i).getquestion().tolowercase() .contains(searchquery)) { sortedquestionset.add(defaultquestinlist.get(i)); } } arraylist<singlequestion> sortedquestionlist = new arraylist<singlequestion>(); sortedquestionlist.addall(sortedquestionset); questionlistadapter = new main_questionlist_adapter(questionactivity.this ,sortedquestionlist); question_list_view.setadapter(questionlistadapter); } else { questions q = c.getquestions(); q.setquestion(defaultquestinlist); questionlistadapter = new main_questionlist_adapter(questionactivity.this ,defaultquestinlist); question_list_view.setadapter(questionlistadapter); } } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } };
above code worked me totally fine .
Comments
Post a Comment