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

问题描述 投票:0回答:2

如何使用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;
    }
}
android listview android-fragments android-edittext android-search
2个回答
0
投票

0
投票

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

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());
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.