There are many solutions provided to this question, but I am not getting exactly where I am going wrong. I have a custom adapter, and I want to filter it.
My adapter class is as follows.
public sendivitesadapter(Context context,ArrayList<Item> items,String[]udis) {
super(context,0,items);
this.context= context;
mCheckedState = new boolean[items.size()];
this.qrusers =(qrusers) context;
this.items = items;
this.udis=uid;
//mContext = context;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Item getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
sendItem ei = (sendItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
final TextView title = (TextView)v.findViewById(R.id.contactname);
final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
//checkBox.setTag(position);
//items.addAll(uid);
checkBox.setTag(udis[position]);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
// isChecked=true;
s=(String)buttonView.getTag();
Log.e("IDDDDDDDD", s);
userid.add(s);
Log.e("Array", userid.toString());
}
else{
s=(String)buttonView.getTag();
userid.remove(s);
}
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(qrusers.this);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("userid", TextUtils.join(",", userid));
editor.commit();
}
});
if (title != null)
title.setText(ei.contactname);
if(subtitle != null)
subtitle.setText(ei.companyname);
}
}
return v;
}
public Filter getFilter() {
if (mFilter1 == null) {
mFilter1 = new ItemsFilter1();
}
return mFilter1;
}
private class ItemsFilter1 extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0){
results.values = items;
results.count = items.size();
}
else{
ArrayList<Item> itemsList1 = new ArrayList<Item>();
for (Item i : items){
if (i.toString().toUpperCase().startsWith(constraint.toString().toUpperCase()))
itemsList1.add(i);
}
results.values = itemsList1;
results.count = itemsList1.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0){
notifyDataSetInvalidated();
}
else{
ArrayList<Item> lst1 = (ArrayList<Item>)results.values;
ArrayList<Item> itemsList1 = new ArrayList<Item>(lst1);
//this.items=mItems;
items =itemsList1;
notifyDataSetChanged();
}
}
}
and in my activity I have used this code.
usersearch1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (usersearch1.getText().toString().equals("")){
sendadapter = new sendivitesadapter(qrusers.this,items1,uid);
listView.setAdapter(sendadapter);
}
s=usersearch1.getText().toString();
Log.e("ALPHABETS", usersearch1.getText().toString());
sendadapter.getFilter().filter(s);
}
There are no errors, but when I type in EditText ,nothing happens. Please let me know I am getting wrong.
add onTextChanged method