【问题标题】:how to search in a custom adapter?如何在自定义适配器中搜索?
【发布时间】:2018-02-28 18:30:58
【问题描述】:

如何使用编辑文本(都在片段上)从列表视图中搜索?我需要使用名称和描述作为搜索选项。我快疯了,请帮帮我!

这是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


    【解决方案1】:

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这部分现在在哪里: public int[] IMAGES = {R.drawable.rai1, ...};公共字符串[] NAMES = {"Rai 1", ...};公共字符串 [] 描述 = {"1", ...};公共字符串[] URL = {"---", ...};我写列表项的部分?
      • 现在你需要创建一个 ImageObj 数组并在构造函数中设置图像、名称、描述和 url
      • 我该怎么做?
      • 像一个普通数组,但带有新的 ImageObj()
      猜你喜欢
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多