本文介绍了如何用用getFilter()的BaseAdapter的ListView过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序已创建一个自定义列表视图,我想实现一个过滤器,这样的列表可以根据在EditText上输入的文本进行过滤。我使用一个BaseAdapter作为一个单独的类和我打电话的类在我的主要活动。我还实施了addTextChangedListener()在我的主要活动,我也实现了用getFilter()在我的BaseAdapter类。但我不知道我怎么可以用用getFilter(),并可以过滤我的列表相应。在列表中,我从一个JSON URL添加值。请帮我让我知道,我怎么能使用用getFilter(),以滤除我的列表中。

In my application I have create a custom list view and I want to implement a filter so that the list can be filtered according to the text entered in the EditText. I am using a BaseAdapter as a separate class and I am calling that class in my Main Activity. I have also implemented the addTextChangedListener() in my main activity and I have also implemented the getFilter() in my BaseAdapter class. But I don't know how can I use getFilter() and can filter my list accordingly. In the list I am adding the values from a JSON URL. Please help me in letting me know that how can I use the getFilter() in order to filter my List.

活动类的code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv = (ListView)findViewById(R.id.listView1);
    et1 = (EditText)findViewById(R.id.editText1);
    inflator = getLayoutInflater();
    et1.addTextChangedListener(this);
    JsonParser jParser = new JsonParser();
    JSONObject json = jParser.getJSONfromUrl(url);
    try
    {
        JSONArray explore = json.getJSONArray("explore");
        for(int i=0; i<explore.length(); i++)
        {
            JSONObject exp = explore.getJSONObject(i);
            list.add(exp.getString("username"));
        }
    }
    catch(JSONException e)
    {
        e.printStackTrace();
    }

    srchadptr = new SearchAdapter(this, inflator, list);
    lv.setAdapter(srchadptr);
}

public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    srchadptr.getFilter().filter(s);
}

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

}

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

}

BaseAdapter类的code:

The code of BaseAdapter class:

public class SearchAdapter extends BaseAdapter implements Filterable {

    Context context;
    LayoutInflater inflater;
    Button btn;
    View vw;
    ArrayList<String> list = new ArrayList<String>();

    public SearchAdapter(Context context,   LayoutInflater inflater, ArrayList<String> list) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.inflater = inflater;
        this.list = list;
    }

    /*public CharSequence filter(CharSequence cs) {
        return cs;
    }*/

    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

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

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



    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LinearLayout ll = (LinearLayout) vw;
        final EditText edt = ((EditText)ll.getChildAt(0));
        vw = inflater.inflate(R.layout.list_items, null);
        ImageView img = (ImageView)vw.findViewById(R.id.imageView1);
        TextView tv = (TextView)vw.findViewById(R.id.textView1);
        btn = (Button)vw.findViewById(R.id.button1);
        tv.setText(String.valueOf(list.get(position)));
        btn.setText(String.valueOf(list.get(position)));
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, list.get(position), Toast.LENGTH_LONG).show();
            }
        });
        return vw;
    }

    public android.widget.Filter getFilter() {
        // TODO Auto-generated method stub
        return new android.widget.Filter() {

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                // TODO Auto-generated method stub

            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // TODO Auto-generated method stub
                return null;
            }
        };
    }
}

在此先感谢...

Thanks in advance...

推荐答案

我希望这个例子可以帮助你

i hope this example could help you

在Main_Activity

in the Main_Activity

    EditText etSearch;
    BaseAdapterFilterable adapter;

    etSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // Listview name of the class
                Listview.this.adapter.getFilter().filter(s);
            }

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

            }

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

            }
        });

在您的适配器把这个类来使用它在用getFilter方法​​

in your adapter put this class to use it in getfilter method

public class filter_here extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // TODO Auto-generated method stub

            FilterResults Result = new FilterResults();
            // if constraint is empty return the original names
            if(constraint.length() == 0 ){
                Result.values = Original_Names;
                Result.count = Original_Names.size();
                return Result;
            }

            ArrayList<String> Filtered_Names = new ArrayList<String>();
            String filterString = constraint.toString().toLowerCase();
            String filterableString;

            for(int i = 0; i<Original_Names.size(); i++){
                filterableString = Original_Names.get(i);
                if(filterableString.toLowerCase().contains(filterString)){
                    Filtered_Names.add(filterableString);
                }
            }
            Result.values = Filtered_Names;
            Result.count = Filtered_Names.size();

            return Result;
        }

        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {
            // TODO Auto-generated method stub
            Names = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }

也从适配器返回的实例 filter_here

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

这篇关于如何用用getFilter()的BaseAdapter的ListView过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 19:48