Android 如何通过在edittext视图中输入文本从包含自定义适配器中的图像和文本的列表视图项中筛选

Android 如何通过在edittext视图中输入文本从包含自定义适配器中的图像和文本的列表视图项中筛选,android,listview,baseadapter,Android,Listview,Baseadapter,我的动机是从包含图像和文本视图的列表视图项目中过滤和显示用户在编辑文本视图中输入的项目。但我无法正确过滤(实现不正确,我猜+Android新手) 我想通过适配器中附加的文本视图对其进行过滤。我能够在列表视图(图像+文本)中正确列出项目。我唯一想要的是在编辑文本中,如果我输入任何单词,它应该过滤列表视图项。 我也曾尝试过同样的方法,但没有成功。 现在我指的是本教程: 任何帮助都将不胜感激。 谢谢 Displaylist.java YouTubeAdapter you; ArrayList<

我的动机是从包含图像和文本视图的列表视图项目中过滤和显示用户在编辑文本视图中输入的项目。但我无法正确过滤(实现不正确,我猜+Android新手)

我想通过适配器中附加的文本视图对其进行过滤。我能够在列表视图(图像+文本)中正确列出项目。我唯一想要的是在编辑文本中,如果我输入任何单词,它应该过滤列表视图项。 我也曾尝试过同样的方法,但没有成功。 现在我指的是本教程:

任何帮助都将不胜感激。 谢谢

  • Displaylist.java

    YouTubeAdapter you;
    ArrayList<String> VideoURL=new ArrayList<String>();
    ArrayList<String> VideoID=new ArrayList<String>();
    ArrayList<String> VideoTitle=new ArrayList<String>();
    ArrayList<String> VideoThumb=new ArrayList<String>();
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_list_view);
    lv=(ListView)findViewById(R.id.lv);
    you=new YouTubeAdapter(DisplayListView.this,VideoURL,VideoTitle);
    lv.setAdapter(you);
    et_search = (EditText) findViewById(R.id.et_search);
    
    et_search.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            String text = et_search.getText().toString().toLowerCase(Locale.getDefault());
            you.filter(text);
        }
    });
    }
    
    YouTubeAdapter你;
    ArrayList VideoURL=新的ArrayList();
    ArrayList VideoID=新的ArrayList();
    ArrayList VideoTitle=新的ArrayList();
    ArrayList VideoThumb=新的ArrayList();
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity\u display\u list\u view);
    lv=(ListView)findViewById(R.id.lv);
    you=newyoutubeadapter(DisplayListView.this、VideoURL、VideoTitle);
    lv.setAdapter(您);
    et_search=(EditText)findViewById(R.id.et_search);
    et_search.addTextChangedListener(新的TextWatcher(){
    @凌驾
    更改前文本之前的公共void(字符序列s、int start、int count、int after){
    }
    @凌驾
    public void onTextChanged(字符序列、int start、int before、int count){
    }
    @凌驾
    公共无效后文本已更改(可编辑){
    String text=et_search.getText().toString().toLowerCase(Locale.getDefault());
    you.filter(文本);
    }
    });
    }
    
  • Adapter.java

    ArrayList<String> mVideo=new ArrayList<String>();
    ArrayList<String> mTitle=new ArrayList<String>();
    ArrayList<String> mThumb=new ArrayList<String>();
    List<String> orig;
    // Filter Class
    public void filter(String charText) {
     charText = charText.toLowerCase(Locale.getDefault());
     orig.clear();
    Log.i(TAGYTAdap,"charText:"+charText);
    if (charText.length() == 0) {
        orig.addAll(mTitle);
    } else {
        for (String wp : mTitle) {
            Log.i(TAGYTAdap,"wp:"+wp);
           if (wp.toLowerCase(Locale.getDefault())
                    .contains(charText)) {
                orig.add(wp);
            }
         }
      }
        notifyDataSetChanged();
    }
    
    ArrayList mVideo=new ArrayList();
    ArrayList mTitle=新的ArrayList();
    ArrayList mThumb=新的ArrayList();
    清单来源;
    //过滤类
    公共空过滤器(字符串图表文本){
    charText=charText.toLowerCase(Locale.getDefault());
    原始清除();
    Log.i(TAGYTAdap,“charText:+charText”);
    如果(ChartText.length()==0){
    原始地址(mTitle);
    }否则{
    用于(字符串wp:mTitle){
    Log.i(TAGYTAdap,wp:+wp);
    if(wp.toLowerCase(Locale.getDefault())
    .contains(图表文本)){
    原始添加(wp);
    }
    }
    }
    notifyDataSetChanged();
    }
    

  • 搜索视图和编辑文本都可以用于此过滤器,但搜索视图更好

    给你,这就行了。!希望如此

    我也做了同样的事情这里是我的代码

     // Filter Function
        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            worldpopulationlist.clear();
            if (charText.length() == 0) {
                worldpopulationlist.addAll(arraylist);
            }
            else
            {
                for (Words wp : arraylist)
                {
                    if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText))
                    {
                        worldpopulationlist.add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }
    
    }
    
    并在主要方面称之为

     // Capture Text in EditText
            editsearch.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                    adapter.filter(text);
                }
    
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                                              int arg2, int arg3) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                    // TODO Auto-generated method stub
                }
            });
    

    不要创建单独的列表,而是创建一个模型类,然后再创建该模型类的列表。