android - 如何在 BaseAdapter 中使用 getFilter() 过滤 ListView

标签 android filter android-listview baseadapter

在我的应用程序中,我创建了一个自定义 ListView ,我想实现一个过滤器,以便可以根据在 EditText 中输入的文本来过滤列表。我将 BaseAdapter 用作单独的类,并在我的主 Activity 中调用该类。我还在我的主要 Activity 中实现了 addTextChangedListener() 并且还在我的 BaseAdapter 类中实现了 getFilter() 。但我不知道如何使用 getFilter() 并相应地过滤我的列表。在列表中,我添加了来自 JSON URL 的值。请帮助我让我知道如何使用 getFilter() 来过滤我的列表。

Activity类代码:

@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类代码:

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

提前致谢...

最佳答案

希望这个例子能帮到你

在 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 方法中使用它

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

关于android - 如何在 BaseAdapter 中使用 getFilter() 过滤 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12456525/

相关文章:

android - Firebase crashlytics 中应用内消息传递崩溃?

r - 如何按行数过滤嵌套的数据框列表并从 R 中的列表中删除过滤的数据框?

android - Cursor Adapter 的用途是什么?

android - 如何从特定项目位置开始显示 ListView

android - 如何在android中以自定义大小保存捕获的图像

android - 是否有可能使用垂直多线 ChipGroup,其中每条线有一个芯片?

php - 使用 PHP 服务器的 Android 聊天应用程序

Google Map 上的 Android GPS 获取/显示位置

python - 基于 GDAL/OGR 的并行空间处理和使用 python 的空间过滤器?

c# - 过滤 ASP.NET ObjectDataSource?