android - 如何在我的自定义 ListView 中应用过滤器?

标签 android listview filter custom-lists

在我的 android 应用程序中,我想在 ListView 上应用过滤器。列表的每一行显示联系人姓名、联系电话类型、联系电话等。 我怎样才能在列表上应用过滤器,所以当我在编辑文本中输入人名时, ListView 必须在文本更改时刷新?我正在使用另一个类来填充列表。

这是我的代码。

AllContactsActivity 类从手机中检索联系人。

public class AllContactsActivity extends ListActivity implements
            android.view.View.OnClickListener, OnItemClickListener {
        EditText sc;
        String name, phonetype;
        ImageButton favourites, contacts, keypad, recent, about;
        int arr, key;
        Cursor tcur;
        int[] typecount, count, id;
        ListView lv;
        ListViewAdapterContacts lva;
        String[] names, numbers, typeinfo, contactinfo, types;
        Integer[] sortlist;
        TreeMap<Integer, String> sorted_set;
        LinkedHashMap<Integer, String> sortedMap;
        ContentResolver tcr;
        HashMap<Integer, String> numbhashmap;
        ArrayAdapter<String> adapter = null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            LinearLayout mainLayout = new LinearLayout(this);
            mainLayout.setOrientation(LinearLayout.VERTICAL);
            LayoutInflater layoutInflater = getLayoutInflater();
            mainLayout.addView(layoutInflater.inflate(R.layout.allcontacts, null));
            mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons, null));
            this.addContentView(mainLayout, params);

            //configureBottomMenu();
            getphones();//get the cantact number
            getContacts();//get contact name,type,id
    //names,types,number,id are populated by above two methods.

            lv = new ListView(getApplicationContext());
            lv = (ListView) findViewById(android.R.id.list);
    //ListViewAdapterContacts class is specified later in the code which populate the list view

            lva = new ListViewAdapterContacts(this, names, types, numbers, id);


            lv.setAdapter(lva);
            lv.setTextFilterEnabled(true);
            lv.setOnItemClickListener(this);

            sc=(EditText)findViewById(R.id.searchcontact);
            sc.addTextChangedListener(textwatcher);


        }// on create
        @Override
        protected void onDestroy() {
            super.onDestroy();
            sc.removeTextChangedListener(textwatcher);
        }

        private TextWatcher textwatcher = new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                AllContactsActivity.this.lva.getFilter().filter(s.toString());
                lva.notifyDataSetChanged();

            }
        };


    }

这里是ListViewAdapterContacts.java

public class ListViewAdapterContacts extends BaseAdapter implements Filterable{

    Activity context;
    String[] names;
    String[] types;
    String[] numbers;
    int[] id;
    public ListViewAdapterContacts(Activity context, String[] names, String[] types, String[] numbers, int[] id) {
        // TODO Auto-generated constructor stub

    this.context=context;
    this.names=names;
    this.types=types;
    this.numbers=numbers;
    this.id=id;
    }

    public int getCount() {
        // TODO Auto-generated method stub 
        if(names==null){
            return 0;
        }
        else{
            return names.length;
        }
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public class viewHolder {
        TextView top;
        TextView bottom;
        TextView downside;
        TextView base;
    }

      public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        viewHolder holder;
        if(convertView==null){

        LayoutInflater inflator=context.getLayoutInflater();
        convertView=inflator.inflate(R.layout.textviewonly,null);

        holder=new viewHolder();
        holder.top=(TextView)convertView.findViewById(R.id.toptext);
        holder.bottom=(TextView)convertView.findViewById(R.id.bottomtext);
        holder.downside=(TextView)convertView.findViewById(R.id.lowest);
        holder.base=(TextView)convertView.findViewById(R.id.baseid);
        convertView.setTag(holder);
        }else{
            holder=(viewHolder)convertView.getTag();
        }
        holder.top.setText(names[position]);
        holder.bottom.setText(types[position]);
        holder.downside.setText(numbers[position]);
        holder.base.setText(""+id[position]);


        View v=(View)convertView;
        //return convertView;
        return v; 
    }


    public Filter getFilter() {
        // TODO Auto-generated method stub
        return null;
    }
}

最佳答案

覆盖 getFilter() 并把这个:

public Filter getFilter() {
  return new MyCustomFilter();
}

当然你必须创建你的自定义过滤器

private class MyCustomFilter extends Filter {
  @Override
  protected FilterResults performFiltering(CharSequence prefix) {
    FilterResults results = new FilterResults();
    // perform your filtering. you'll need to create a new List to get the correct filtered results. then
    results.values = (your new List);
    results.count = (your new List).size();

    return results;
  @Override
  protected void publishResults(CharSequence constraint, FilterResults results) {
    List filtered = (ArrayList)results.values;
      // set the data of your adapter here

      if(results.count>0) {
        notifyDataSetChanged();
      } else {
        notifyDataSetInvalidated();
      }
  }
}

关于android - 如何在我的自定义 ListView 中应用过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8353535/

相关文章:

android - 在 ListView 中获取选定行的背景颜色

Android Drop up(?)菜单可能吗?

php - 如何按值过滤多维数组(超过两级)?

python - Django case when else in filter

java.net.协议(protocol)异常 : method does not support a request body: GET

android - 是否可以为 android studio 中的编辑器设置默认缩放比例?

java - 始终从启动器 Activity 启动应用程序

android - 使用聚合调用 Google Fit REST API

android - fragment 中的 ListView 不滚动

elasticsearch - ElasticSearch高亮显示过滤查询