过滤扩展BaseAdapter的ListView

时间:2018-03-26 17:39:59

标签: android listview filter adapter baseadapter

我到处搜索但没有解决方案。

我试图过滤适配器" appsAdapter"方法:

appsAdapter.getFilter().filter();

这适用于MainActivity上的ListView。

这是我的适配器:

class AppsAdapter extends BaseAdapter implements Filterable{

private Context mContext;
private List<Pair<String, List<String>>> mAppsWithPermission;
private ArrayFilter mFilter;


AppsAdapter(Context context, List<Pair<String, List<String>>> appsWithPermission) {
    mContext = context;
    mAppsWithPermission = appsWithPermission;
}

static class ViewHolder {
    TextView appName;
}

@Override
public int getCount() {
    return mAppsWithPermission.size();
}

@Override
public Object getItem(int position) {
    return mAppsWithPermission.get(position);
}

@Override
public long getItemId(int position) {
    return mAppsWithPermission.get(position).hashCode();
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();
        holder.appName = convertView.findViewById(R.id.list_item_appname);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final Pair<String, List<String>> item = mAppsWithPermission.get(position);

    final PackageManager packageManager = mContext.getPackageManager();
    String name = null;
    try {
        name = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(item.first, PackageManager.GET_META_DATA));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }


public Filter getFilter() {
    // TODO Auto-generated method stub
    if (mFilter == null) {
        mFilter = new ArrayFilter();
    }
    return mFilter;
}
return convertView;
}

private class ArrayFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        //?
        return null;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        //?
    }

}
}

我想过滤名字。

正如我之前所说,我已经尝试了所有,但似乎没有任何效果,所以请不要将此标记为&#34;重复&#34;。

提前感谢您的帮助,

此致

Faby

0 个答案:

没有答案