Android : Android: ListView Filter is not working exactly

on Wednesday, October 1, 2014


I'am listing differnt data from the server. Consider i have 5 different type of data in my list. I want to filter the list based on one data. For that I have written the following code.



eventFilter = (EditText) rootView.findViewById(R.id.eventFilter);
eventFilter.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

}


@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
search_data = s.toString();
}
});
Button searchbtn = (Button) rootView.findViewById(R.id.searchbtn);
searchbtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
search = new SearchFilter(context, cts);
search.getFilter().filter(search_data);
}
});


In the above code just am passing the text value to SearchFilter.



@Override
public Filter getFilter() {
// TODO Auto-generated method stub
if(cFilter==null)
{
cFilter=new CustomFilter();
}
return cFilter;
}
public class CustomFilter extends Filter {

@Override
protected FilterResults performFiltering(CharSequence prefix) {

FilterResults results = new FilterResults();

if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<CalEvent>(event);
}
}

if (prefix == null || prefix.length() == 0) {
ArrayList<CalEvent> list;
synchronized (mLock) {
list = new ArrayList<CalEvent>(mOriginalValues);
}
results.values = list;
results.count = list.size();
} else {
String prefixString = prefix.toString().toLowerCase();


ArrayList<CalEvent> values;
synchronized (mLock) {
values = new ArrayList<CalEvent>(mOriginalValues);
}

final int count = values.size();
final ArrayList<CalEvent> newValues = new ArrayList<CalEvent>();

for (int i = 0; i < count; i++) {
CalEvent value=values.get(i);
EventType type = EventType.getEventType(value.event_type);
String valueText = type.name.toLowerCase();
if (valueText.indexOf(prefixString)!=-1) {
newValues.add(value);
Toast.makeText(ctx, value.toString(), Toast.LENGTH_SHORT).show();
Log.i("value.toString",value.toString());
} else {
ProjectEventFragment.cts.clear();
ProjectEventFragment.adapter.notifyDataSetChanged();
}
}

results.values = newValues;
results.count = newValues.size();
}

return results;
}


Here if am typing some data which is not available in the list then the list should be empty or if available then it should list based on the available data. If the data is not available then i am doing clear and setting to notifyDataSetChanged. The list is removing all the data when i type non available data. But again when i type the available data then it is not showing all the data.


I don't know where am doing wrong. Please do correct me.


0 comments:

Post a Comment