spent hours trying work no avail :( have set searchfilter listview, works fine. each has checkbox - when check checkbox either before or after filtering, not being remembered makes filter pointless. appreciate guidance here.
my code:
import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.searchview; import android.widget.listview; import java.util.arraylist; import java.util.arrays; public class mainactivity extends appcompatactivity implements searchview.onquerytextlistener { private allstuffmodel[] modelitems = allstuff.allconditions; private listview conditionsview; private searchview search_view; private allstuffadapter adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); adapter = new allstuffadapter(this, new arraylist<allstuffmodel>(arrays.aslist(modelitems)), this); conditionsview = (listview) findviewbyid(r.id.condition_list); conditionsview.setadapter(adapter); search_view = (searchview) findviewbyid(r.id.search_view); search_view.setonquerytextlistener(this); } @override public boolean onquerytextchange(string newtext) { adapter.getfilter().filter(newtext); return false; } @override public boolean onquerytextsubmit(string query) { return false; } }
allstuffadapter.java
import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.filter; import android.widget.filterable; import android.widget.textview; import java.util.arraylist; import java.util.list; public class allstuffadapter extends arrayadapter<allstuffmodel> implements filterable { private list<allstuffmodel> modelitems; private list<allstuffmodel> filteredmodelitems; private context context; private mainactivity mainactivity; private valuefilter valuefilter; @override public boolean hasstableids() { return true; } public allstuffadapter(context context, list<allstuffmodel> resource, mainactivity mainactivity) { super(context, r.layout.condition_row, resource); this.context = context; this.modelitems = resource; this.mainactivity = mainactivity; filteredmodelitems = modelitems; } @override public view getview(final int position, view convertview, viewgroup parent) { layoutinflater inflater = ((activity) context).getlayoutinflater(); convertview = inflater.inflate(r.layout.condition_row, parent, false); textview conditiontext = (textview) convertview.findviewbyid(r.id.condition_text); final checkbox conditioncheckbox = (checkbox) convertview.findviewbyid(r.id.condition_checkbox); conditioncheckbox.setid(position); conditiontext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (!conditioncheckbox.ischecked()) { conditioncheckbox.setchecked(true); modelitems.get(position).setischecked(true); } else if(conditioncheckbox.ischecked()) { conditioncheckbox.setchecked(false); modelitems.get(position).setischecked(false); } system.out.println( modelitems.get(position).ischecked()); } }); conditiontext.settext(modelitems.get(position).getname()); conditioncheckbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkbox currentcheckbox = (checkbox) v; if (currentcheckbox.ischecked()) { // if checked set true, else false modelitems.get(position).setischecked(true); } else if (!currentcheckbox.ischecked()) { modelitems.get(position).setischecked(false); } system.out.println( modelitems.get(position).ischecked()); } }); return convertview; } @override public int getcount() { return modelitems.size(); } @override public long getitemid(int position) { return modelitems.indexof(getitem(position)); } @override public filter getfilter() { if (valuefilter == null) { valuefilter = new valuefilter(); } return valuefilter; } private class valuefilter extends filter { @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); if (constraint != null && constraint.length() > 0) { arraylist<allstuffmodel> filterlist = new arraylist<allstuffmodel>(); (int = 0; < filteredmodelitems.size(); i++) { if ((filteredmodelitems.get(i).getname().touppercase()) .contains(constraint.tostring().touppercase())) { allstuffmodel allconditionsmodel = new allstuffmodel(filteredmodelitems.get(i).getcode(),filteredmodelitems.get(i).getname()); allconditionsmodel.setischecked(filteredmodelitems.get(i).ischecked()); filterlist.add(allconditionsmodel); } } results.count = filterlist.size(); results.values = filterlist; } else { results.count = filteredmodelitems.size(); results.values = filteredmodelitems; } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { modelitems = (arraylist<allstuffmodel>) results.values; notifydatasetchanged(); } } }
allstuffmodel.java
public class allstuffmodel { private string name; private string code; private boolean ischecked; public allstuffmodel(string code, string name) { this.name = name; this.code = code; } public string getname() { return this.name; } public string getcode() { return this.code; } public boolean ischecked() { return ischecked; } public void setischecked(boolean ischecked) { this.ischecked = ischecked; } } allstuff.java public class allstuff { private allstuff() { } public static final allstuffmodel[] allconditions = { new allstuffmodel("ca", "candy"), new allstuffmodel("fo", "football"), new allstuffmodel("mu", "music"), new allstuffmodel("sn", "snooker"), }; }
condition_row.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingtop="10dp"> <checkbox android:id="@+id/condition_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginright="7dp" /> <textview android:id="@+id/condition_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" android:textcolor="#de000000" android:textsize="17sp" /> </linearlayout> activity_main.xml <?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/scrollview_main" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <searchview android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="2dp" android:queryhint="search...." /> <listview android:id="@+id/condition_list" android:layout_width="wrap_content" android:layout_height="wrap_content"></listview> </linearlayout> </scrollview>
what have tried use notifydatasetchaanged()
in onclick methods in allstuffadapter
, stops checkbox being checked completely.
tl/dr: how can remember checkboxes checked search filter?
try changing
conditioncheckbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkbox currentcheckbox = (checkbox) v; if (currentcheckbox.ischecked()) { // if checked set true, else false modelitems.get(position).setischecked(true); } else if (!currentcheckbox.ischecked()) { modelitems.get(position).setischecked(false); } system.out.println( modelitems.get(position).ischecked()); } });
to
conditioncheckbox.setoncheckedchangelistener(null); conditioncheckbox.setchecked(modelitems.get(position).ischecked()); conditioncheckbox.setoncheckedchangelistener(new oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { modelitems.get(position).setischecked(ischecked); } });
Comments
Post a Comment