问题
i'm try to write simple filtering ListView by this below code. but that does not work correctly
in this code i'm using ContactListStructure
class structure as:
public class ContactListStructure implements Serializable {
public Long id;
public String name;
public String mobile;
public Bitmap photo;
public Boolean checked;
...
}
and i'm fill this class as an ArrayList with phone contacts. after fill that i'm set this list into ListView without any problem,then i'm try to fillter this list by:
/* private ArrayList<ContactListStructure> contact_item; */
contactsAdapter = new ContactsAdapter ( G.contact_item , ActivityContactList.this);
lstContent.setAdapter ( contactsAdapter );
search_contacts.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
contactsAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
POST UPDATE
search_contacts
is an EditText
and after type into that ListView dont filter and dont any change. my BaseAdapter :
public class ContactsAdapter extends BaseAdapter implements Filterable {
private Context mCtx=null;
private ArrayList<ContactListStructure> mData=null;
private ArrayList<ContactListStructure> arraylist;
private List<ContactListStructure> worldpopulationlist = null;
private ItemFilter mFilter = new ItemFilter();
static int i=0;
public ContactsAdapter (ArrayList<ContactListStructure> contact_item, Context ctx) {
mData=contact_item;
mCtx=ctx;
this.worldpopulationlist = contact_item;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public ContactListStructure getItem(int position) {
return worldpopulationlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater inflator=((Activity)mCtx).getLayoutInflater();
convertView=inflator.inflate(R.layout.send_sms,null);
}
CheckBox chk_name_mobile = (CheckBox)convertView.findViewById(R.id.chk_name_mobile);
ImageView photo = (ImageView)convertView.findViewById(R.id.photo);
String name=mData.get(position).name;
chk_name_mobile.setText(name);
if( mData.get(position).photo == null )
photo.setImageDrawable( G.context.getResources().getDrawable(R.drawable.user) );
else
photo.setImageBitmap(mData.get(position).photo);
return convertView;
}
@Override
public Filter getFilter () {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString();
FilterResults results = new FilterResults();
final ArrayList<ContactListStructure> list = mData;
int count = list.size();
final ArrayList<ContactListStructure> nlist = new ArrayList<ContactListStructure>(count);
String filterableString ;
for (ContactListStructure wp : worldpopulationlist)
{
if (wp.getName().contains(filterString))
{
//Log.e ("wp: ", String.valueOf ( wp ) );
ContactListStructure item = new ContactListStructure();
item.id = wp.id;
item.name = wp.name;
item.mobile = wp.mobile;
item.photo = wp.photo;
nlist.add(item);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
arraylist = (ArrayList<ContactListStructure>) results.values;
notifyDataSetChanged();
}
}
}
UPDATE POST:
after help in comments my notifyDataSetChanged
not working. after type into edit text thats can be find but listview dont refresh.
回答1:
You can try following this question here: ListView is blank while using getFilter function
It was able to filter based on a choice from the ListView.
回答2:
I think you forgot to add the data returned from result to your mData
:
protected void publishResults(CharSequence constraint, FilterResults results) {
arraylist = (ArrayList<ContactListStructure>) results.values;
mData.clear();
for(int i = 0, i < arraylist.size(); i++)
mData.add(arraylist.get(i));
notifyDataSetChanged();
}
}
来源:https://stackoverflow.com/questions/27272630/android-filter-listview-by-baseadapter