database - Android listview with filter issue -
i have listview of items coming database. have put edit text @ top of listview. want when user start typing in edit text, listview should filtered. example, if user enters letter "a", names starting "a" should appear in list. have used custom adapter in code. asked question many times, haven't found solution yet.
this data list activity code:
public class datalistactivity extends activity { listview listview; sqlitedatabase sqlitedatabase; fooddbhelper fooddbhelper; cursor cursor; listdataadapter listdataadapter; private button button1; listdataadapter dataadapter = null; button button; dataprovider dataprovider; arraylist<hashmap<string, string>> namesslist; edittext inputsearch; string search_name; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.data_list_layout); inputsearch = (edittext)findviewbyid(r.id.inputsearch); inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // when user changes text listdataadapter.getfilter().filter(cs); } @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } @override public void aftertextchanged(editable arg0) { // todo auto-generated method stub } }); listview = (listview) findviewbyid(r.id.list_view); listdataadapter = new listdataadapter(getapplicationcontext(), r.layout.row_layout) { @override protected void showcheckedbutton(int position, boolean value) { // todo auto-generated method stub dataprovider item = (dataprovider) listdataadapter .getitem(position); log.i("", ""); item.setselected(value); button mybutton = (button) findviewbyid(r.id.findselected); mybutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { stringbuffer responsetext = new stringbuffer(); responsetext .append("the following dishes selected...\n"); arraylist<dataprovider> list = listdataadapter .getselectedindexes(); int sum = 0; (int = 0; < list.size(); i++) { dataprovider dataprovider = list.get(i); sum = sum + dataprovider.getcalorie(); responsetext.append("\n" + dataprovider.getname() + " : " + dataprovider.getcalorie() + " kcal" ); } toast.maketext(getapplicationcontext(), ""+responsetext+"\n"+"................................." +"\n"+"total calories in menu : " +sum, toast.length_long).show(); } }); } }; listview.setadapter(listdataadapter); fooddbhelper = new fooddbhelper(getapplicationcontext()); sqlitedatabase = fooddbhelper.getreadabledatabase(); cursor = fooddbhelper.getinformations(sqlitedatabase); if (cursor.movetofirst()) { { string name, quantity, fat, protein, sugar, vitamins; boolean selected = false; string names = null; integer calorie; name = cursor.getstring(0); quantity = cursor.getstring(1); calorie = integer.valueof(cursor.getstring(2)); fat = cursor.getstring(3); protein = cursor.getstring(4); sugar = cursor.getstring(5); vitamins = cursor.getstring(6); dataprovider dataprovider = new dataprovider(name, quantity, calorie, fat, protein, sugar, vitamins, names, selected); listdataadapter.add(dataprovider); } while (cursor.movetonext()); } listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string name = (string) ((textview) view .findviewbyid(r.id.text_dish_name)).gettext(); string quantity = (string) ((textview) view .findviewbyid(r.id.text_dish_quantity)).gettext(); string calorie = (string) ((textview) view .findviewbyid(r.id.text_dish_calorie)).gettext(); string fat = (string) ((textview) view .findviewbyid(r.id.text_dish_fat)).gettext(); string protein = (string) ((textview) view .findviewbyid(r.id.text_dish_protein)).gettext(); string sugar = (string) ((textview) view .findviewbyid(r.id.text_dish_sugar)).gettext(); string vitamins = (string) ((textview) view .findviewbyid(r.id.text_dish_vitamins)).gettext(); string.valueof(parent.getitematposition(position)); toast.maketext(getapplicationcontext(), "dish name : " + name, toast.length_short).show(); intent intent = new intent(getapplicationcontext(), detail.class); intent.putextra("dish name", name); intent.putextra("dish quantity", quantity); intent.putextra("dish calorie", calorie); intent.putextra("dish fat", fat); intent.putextra("dish protein", protein); intent.putextra("dish sugar", sugar); intent.putextra("dish vitamins", vitamins); startactivity(intent); } }); } public void gobackk(view view) { intent intent = new intent(this, mainmenu.class); startactivity(intent); }
use addtextchangedlistener()
method of edittext. this
inputsearch = (edittext)findviewbyid(r.id.edittext); .... .... listview.setadapter(listdataadapter); inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // when user changes text listdataadapter.getfilter().filter(cs); } @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } @override public void aftertextchanged(editable arg0) { // todo auto-generated method stub } });
Comments
Post a Comment