Bypassing The Bug In Filtering That Does Not Update Listview Android
Solution 1:
1) Backspace Filtering
In your publishResults
, you are modifying your pagelist
, so if you press backspace and filter again, pagelist
only contains a small portion of the original pagelist
entries:
So, you need to keep a copy of your original data! Something like:
List<String> allData;
PagesAdapter(Context c) {
...
allData = pagelist;
...
}
and in your filter method, use the allData object:
for (int i = 0; i < allData.size(); i++) {
filterableString = allData.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
Filtered_Names.add(filterableString);
}
}
and also where you reset your data back to the original:
// if constraint is empty return the original namesif (constraint.length() == 0) {
Result.values = allData;
Result.count = allData.size();
returnResult;
}
2) Clear filter after returning from another activity:
This can be done in onResume... Personally, i would clear the text in the editText as to reset the filter like so:
@OverrideprotectedvoidonResume() {
if (editTextB != null) {
editTextB.setText("");
}
super.onResume();
}
Solution 2:
Finally found the bug of the backspace, you had some error in the filter function, that am returning an ArrayList, instead of the List that holds the variable the code should the final code is:
publicclassIndexPageActivityextendsActivityimplementsOnItemClickListener{
ListView listView;
EditText editTextB;
PagesAdapter adapter1;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
adapter1 = newPagesAdapter(this);
listView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listView.setOnItemClickListener(this);
editTextB.addTextChangedListener(newTextWatcher() {
@OverridepublicvoidonTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
IndexPageActivity.this.adapter1.getFilter().filter(cs.toString());
adapter1.notifyDataSetChanged();
}
@OverridepublicvoidbeforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@OverridepublicvoidafterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
@OverridepublicvoidonItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
Intent i;
Stringname= adapter1.getItem(position);
Log.d("id", name);
if (name.equals("Item1"))
{
i = newIntent(this, anActivity.class);
startActivity(i);
}
elseif (name.equals("Item2"))
{
i = newIntent(this, anActivity2.class);
startActivity(i);
}
}
}
classSingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
classPagesAdapterextendsBaseAdapterimplementsFilterable{
ArrayList<String> pagelist;
List<String> arrayList;
Context context;
String [] pagedatas;
PagesAdapter(Context c){
context=c;
pagelist = newArrayList<String>();
Resourcesres= c.getResources();
pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(pagedatas[i]);
}
arrayList = pagelist;
}
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn arrayList.size();
}
@OverridepublicvoidnotifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Overridepublic String getItem(int i) {
// TODO Auto-generated method stubreturn arrayList.get(i);
}
@OverridepubliclonggetItemId(int i) {
// TODO Auto-generated method stubreturn i;
}
@Overridepublic View getView(int i, View view, ViewGroup viewG) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextViewpagetitle= (TextView) row.findViewById(R.id.textViewRow);
String temp=arrayList.get(i);
pagetitle.setText(temp);
return row;
}
@Overridepublic Filter getFilter() {
// TODO Auto-generated method stubFilterfilter=newFilter() {
@SuppressWarnings("unchecked")@OverrideprotectedvoidpublishResults(CharSequence constraint, FilterResults results) {
arrayList = (List<String>) results.values;
notifyDataSetChanged();
}
@Overrideprotected FilterResults performFiltering(CharSequence constraint) {
FilterResultsresults=newFilterResults();
ArrayList<String> FilteredArrayNames = newArrayList<String>();
if (pagelist == null) {
pagelist = newArrayList<String>(arrayList);
}
if (constraint == null || constraint.length() == 0) {
results.count = pagelist.size();
results.values = pagelist;
} else {
constraint = constraint.toString().toLowerCase();
for (inti=0; i < pagelist.size(); i++) {
StringdataNames= pagelist.get(i);
if (dataNames.toLowerCase().startsWith(constraint.toString())) {
FilteredArrayNames.add(dataNames);
}
}
results.count = FilteredArrayNames.size();
System.out.println(results.count);
results.values = FilteredArrayNames;
Log.e("VALUES", results.values.toString());
}
return results;
}
};
return filter;
}
}
Solution 3:
How about putting
adapter1.notifyDataSetChanged();
in onResume() method of the Activity?
Post a Comment for "Bypassing The Bug In Filtering That Does Not Update Listview Android"