如何在自定义适配器中搜索?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在自定义适配器中搜索?相关的知识,希望对你有一定的参考价值。

如何使用edittext(在片段上)从列表视图中搜索?我需要使用NAMES和DESCRIPTIONS作为搜索选项。我疯了,请帮帮我吧!

这是listview的自定义适配器类的代码:

public class CustomAdapter extends BaseAdapter implements Filterable {
    private Context context;

    public int[] IMAGES = {R.drawable.rai1, ...};

    public String[] NAMES = {"Rai 1", ...};

   public String[] DESCRIPTIONS = {"1", ...};

    public String[] URL = {"http://www.---", ...};

   public CustomAdapter (Context c){context = c;}
    @Override
    public int getCount() {return IMAGES.length;}
    @Override
    public Object getItem(int position) {return URL[position];}
    @Override
    public long getItemId(int position) {return 0;}
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.custom_layout, null);
        ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
        TextView textView_nome = (TextView)view.findViewById(R.id.textView_nome);
        TextView textView_descrizione = (TextView)view.findViewById(R.id.textView_descrizione);
        imageView.setImageResource(IMAGES[position]);
        textView_nome.setText(NAMES[position]);
        textView_descrizione.setText(DESCRIPTIONS[position]);
        return view;
    }

    @Override
    public Filter getFilter() {
        return null;
    }
}
答案

有关Stack溢出问题的帖子很多。探索这些帖子

Textwatcher with custom listview in android

How to Filter ListView through EditText

List View Filter Android

另一答案

您可以使用对象传输数据来进行自定义搜索实现

public class CustomAdapter extends BaseAdapter {

    ImageObj[] objs=new ImageObj[]{new ImageObj(...)};
    List<ImageObj> list= Arrays.asList(objs);

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position).url;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.custom_layout, null);
        ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
        TextView textView_nome = (TextView)view.findViewById(R.id.textView_nome);
        TextView textView_descrizione = (TextView)view.findViewById(R.id.textView_descrizione);
        imageView.setImageResource(list.get(position).image);
        textView_nome.setText(list.get(position).name);
        textView_descrizione.setText(list.get(position).description);
        return view;
    }

    //Clear the last search and filter
    void filter(String query){
        list.clear();
        for (ImageObj imageObj:objs){
            if (imageObj.isValid(query))
                list.add(imageObj);
        }
        notifyDataSetChanged();
    }

    class ImageObj{
        int image;
        String name;
        String description;
        String url;

        public ImageObj(int image, String name, String description, String url) {
            this.image = image;
            this.name = name;
            this.description = description;
            this.url = url;
        }

        boolean isValid(String query){
            return name.toLowerCase().contains(query.toLowerCase())||description.toLowerCase().contains(query.toLowerCase());
        }
    }
}

以上是关于如何在自定义适配器中搜索?的主要内容,如果未能解决你的问题,请参考以下文章

在自定义适配器类中发送服务器请求后,如何更改 Listview 按钮名称?

如何在自定义适配器上实现 onClickListener?

在自定义适配器上使用.getFilter()时出现问题(未正确过滤)(Android)

如何在自定义适配器中使用 String[]?

如何在自定义 ListView 适配器中处理 RadioGroup 的 onCheckedChangeListener

在自定义 Listview 适配器中使用多个视图