我有自定义 ArrayAdapter:
public class CallAdapter extends ArrayAdapter<CallItem> implements Filterable{
View view;
ContactHolder holder = null;
int color = 0;
Context context;
int layoutResourceId;
public static int rowHeight = 0;
private ViewSwitcher switcher;
List<CallItem> contacts = null;
Map<ContactItem, View> contactViewMap = ContactsViewMapSingletone.getInstance().getOrderViewMap();
private ArrayList<CallItem> filtredContacts;
private Filter filter;
public CallAdapter(Context context, int layoutResourceId,List<CallItem> contacts) {
super(context, layoutResourceId, contacts);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.contacts = contacts;
this.filtredContacts = new ArrayList<CallItem>();
this.filtredContacts.addAll(contacts);
}
@Override
public CallItem getItem(int arg0) {
return contacts.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
/** * метод для привязки элементов UI к данным конкретного контакта */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
view = row;
if(row == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.call_item, parent, false);
holder = new ContactHolder();
holder.contactIcon = (ImageView)row.findViewById(R.id.contactIcon);
holder.contactName = (CustomTextView)row.findViewById(R.id.contactName);
holder.contactNumber = (CustomTextView)row.findViewById(R.id.contactNumber);
holder.contactDays = (CustomTextView)row.findViewById(R.id.contactDays);
row.setTag(holder);
}
else
{
holder = (ContactHolder)row.getTag();
}
CallItem contact = contacts.get(position);
try
{
holder.contactIcon.setBackgroundResource(contact.getIcon());
holder.contactName.setText(contact.getName());
holder.contactNumber.setText(contact.getDays());
}
catch(Exception e)
{
e.printStackTrace();
}
return row;
}
@Override
public Filter getFilter()
{
if (filter == null)
filter = new CallFilter();
return filter;
}
static class ContactHolder
{
ImageView contactIcon;
CustomTextView contactName;
CustomTextView contactNumber;
CustomTextView contactDays;
}
@SuppressLint("DefaultLocale")
private class CallFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
Log.i(" filtered value", constraint.toString());
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix == null || prefix.length() == 0)
{
results.values = contacts;
results.count = contacts.size();
}
else
{
final ArrayList<CallItem> list = new ArrayList<CallItem>();
synchronized (this)
{
list.addAll(contacts);
}
filtredContacts.clear();
int count = list.size();
for (int i=0; i<count; i++)
{
final CallItem pkmn = list.get(i);
if(pkmn.getName().toLowerCase(Locale.getDefault()).contains(constraint))
{
Log.i("found pref",pkmn.toString() );
filtredContacts.add(pkmn);
}
}
Log.i("prefix",prefix);
for(int i = 0, l = filtredContacts.size(); i < l; i++)
{
Log.i("filtered list",filtredContacts.get(i).toString());
}
results.values = filtredContacts;
results.count = filtredContacts.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtredContacts = (ArrayList<CallItem>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = filtredContacts.size(); i < l; i++)
{
add(filtredContacts.get(i));
}
notifyDataSetChanged();
}
}
}
但我需要过滤项目不是按名称,而是按图标。所以用户选择应该过滤哪些项目的图标,并查看项目列表,这些项目在顶部有这个图标。我试图在中实现这个(将 int 作为参数传递),performFiltering
但它必须获取CharSequence
参数。而且我需要实现此功能,而不是像往常一样在 EditText 中键入值,而是在另一个 Activity 中获取 icon int 并返回我的列表并查看过滤的项目。对不起我的英语不好。