android如何在自定义适配器上启用过滤功能?

时间:2014-07-23 07:25:46

标签: android search filter adapter

我有一个自定义适配器,显示每个listitem的复选框的国家/地区

但是当我尝试启用搜索功能时,它不起作用

这是我自定义适配器的代码

private class MyCustomAdapter extends ArrayAdapter<Country> 
{

    private ArrayList<Country> stateList;

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<Country> stateList) 
    {
        super(context, textViewResourceId, stateList);
        this.stateList = new ArrayList<Country>();
        this.stateList.addAll(stateList);
    }

    private class ViewHolder 
    {
        TextView arabic;
        TextView english;
        TextView id;
        CheckBox check;
    }

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

        ViewHolder holder = null;

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) 
        {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.country_list_item, null);

            holder = new ViewHolder();
            holder.arabic = (TextView) convertView.findViewById(R.id.tv_Arabic_name);
            holder.english = (TextView) convertView.findViewById(R.id.tv_english_name);
            holder.id = (TextView) convertView.findViewById(R.id.tv_id);
            holder.check = (CheckBox) convertView.findViewById(R.id.check_star);

            convertView.setTag(holder);

            holder.check.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {
                    final CheckBox cb = (CheckBox) v;

                    final Country _state = (Country) cb.getTag();

                    final String a = _state.getId();

                    String country_name = _state.getE();


                    AlertDialog.Builder alert = new AlertDialog.Builder(c);
                    alert.setCancelable(false);


                    if (cb.isChecked()) 
                    {
                        dbObject.open();
                        dbObject.updateEntry(a, "1");
                        _state.setS("1");
                        dbObject.close();

                        int pref_count = prefs.getInt("sp", 0);
                        pref_count++;
                        prefs.edit().putInt("sp", pref_count).commit();

                        cb.setChecked(true);

                        Toast.makeText(getApplicationContext(), country_name + " Added To Favorite!", Toast.LENGTH_SHORT).show();
                    } 

                    else 
                    {
                        dbObject.open();
                        dbObject.updateEntry(a, "0");
                        _state.setS("0");
                        dbObject.close();

                        int pref_count = prefs.getInt("sp", 0);
                        pref_count--;
                        prefs.edit().putInt("sp", pref_count).commit();

                        cb.setChecked(false);
                        Toast.makeText(getApplicationContext(), country_name + " Removed From Favorite!", Toast.LENGTH_SHORT).show();                   

                    }

                }
            });

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

        Country county_obj = coutriesArrayList.get(position);

        holder.arabic.setText(county_obj.getA());
        holder.english.setText(county_obj.getE());
        holder.id.setText(county_obj.getId());

        int TOF = Integer.parseInt(county_obj.getS());
        boolean isCheckedInDB = (TOF != 0);
        holder.check.setChecked(isCheckedInDB);

        holder.check.setTag(county_obj);

        return convertView;
    }

}

我的搜索功能

inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    MainActivity.this.adapter.getFilter().filter(cs);   
}

@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                          
}

});

1 个答案:

答案 0 :(得分:0)

最后我找到了怎么做。而且它很好

@Override
public Filter getFilter() 
{
    if (planetFilter == null)
        planetFilter = new PlanetFilter();

    return planetFilter;
}

private class PlanetFilter extends Filter 
{

    @Override
    protected FilterResults performFiltering(CharSequence constraint) 
    {
        FilterResults results = new FilterResults();
        // We implement here the filter logic
        if (constraint == null || constraint.length() == 0) 
        {
            // No filter implemented we return all the list
            results.values = origPlanetList;
            results.count = origPlanetList.size();
        }
        else 
        {
            // We perform filtering operation
            List<Country> nPlanetList = new ArrayList<Country>();

            for (Country p : planetList) 
            {
                if (p.getE().toUpperCase()
                        .startsWith(constraint.toString().toUpperCase()))
                    nPlanetList.add(p);
            }

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

        }
        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) 
    {

        // Now we have to inform the adapter about the new list filtered
        if (results.count == 0)
            notifyDataSetInvalidated();
        else 
        {
            planetList = (List<Country>) results.values;
            notifyDataSetChanged();
        }

    }

}
相关问题