安装了具有搜索功能的应用程序列表

时间:2015-06-18 06:19:24

标签: android listview

我创建了已安装应用的Listview及其显示列表都正确无误。现在我想给它提供搜索功能。列表包含appIcon,appName和复选框。 这是我的主要活动:

apps = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < apps.size(); i++) {
        PackageInfo p = apps.get(i);

        AppInfo newInfo = new AppInfo();
        newInfo.appname = p.applicationInfo.loadLabel(getPackageManager())
                .toString();
        newInfo.pname = p.packageName;
        newInfo.versionName = p.versionName;
        newInfo.versionCode = p.versionCode;
        newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
        newInfo.status = selectedApps.get(p.packageName) == null ? false
                : true;

        res.add(newInfo);
    }

ed_search.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            Log.d("sss", "text to search:" + cs.toString());
            adapter.getFilter().filter(cs.toString());   
            adapter.notifyDataSetChanged();
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }
    });

这是我的自定义适配器:

public class CustomListAdapter extends ArrayAdapter<AppInfo>implements CompoundButton.OnCheckedChangeListener, Filterable {
SparseBooleanArray mCheckStates;

Context context;
int layoutResourceId;
ArrayList<AppInfo> data = null;
ArrayList<AppInfo> filteredData = null;
List<String> savedList;
private ItemFilter mFilter = new ItemFilter();

public CustomListAdapter(Context context, int layoutResourceId, ArrayList<AppInfo> res) {
    super(context, layoutResourceId, res);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = res;
    mCheckStates = new SparseBooleanArray(res.size());
    int i = 0;
    for(AppInfo a: res){
        mCheckStates.put(i++, a.status);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    AppInfoHolder holder = null;

    if (row == null) {

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new AppInfoHolder();

        holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
        holder.txtTitle = (TextView) row.findViewById(R.id.textView1);
        holder.chkSelect = (CheckBox) row.findViewById(R.id.checkBox1);

        row.setTag(holder);

    } else {
        holder = (AppInfoHolder) row.getTag();
    }

    AppInfo appinfo = data.get(position);
    holder.txtTitle.setText(appinfo.appname);
    holder.imgIcon.setImageDrawable(appinfo.icon);
    // holder.chkSelect.setChecked(true);
    holder.chkSelect.setTag(position);
    holder.chkSelect.setChecked(mCheckStates.get(position));
    holder.chkSelect.setOnCheckedChangeListener(this);
    return row;

}

public boolean isChecked(int position) {
    return mCheckStates.get(position, false);
}

public void setChecked(int position, boolean isChecked) {
    mCheckStates.put(position, isChecked);

}

public void toggle(int position) {
    setChecked(position, !isChecked(position));

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    mCheckStates.put((Integer) buttonView.getTag(), isChecked);

}

static class AppInfoHolder {
    ImageView imgIcon;
    TextView txtTitle;
    CheckBox chkSelect;

}
public Filter getFilter() {
    return mFilter;
}

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final List<AppInfo> list = data;

        int count = list.size();
        final ArrayList<AppInfo> nlist = new ArrayList<AppInfo>(count);

        AppInfo filterableString ;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i);
            if (filterableString.toString().toLowerCase().contains(filterString)) {
                nlist.add(filterableString);
            }
        }

        results.values = nlist;
        results.count = nlist.size();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<AppInfo>) results.values;
        notifyDataSetChanged();
    }

}

我想在ed_search edittext上应用搜索。

0 个答案:

没有答案