【问题标题】:How to filter from list view items containing image and text in custom adapter by entering text in edittext view如何通过在edittext视图中输入文本来过滤自定义适配器中包含图像和文本的列表视图项目
【发布时间】:2017-03-10 13:57:47
【问题描述】:

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

我想通过适配器中附加的文本视图对其进行过滤。我能够在列表视图(图像+文本)中正确列出项目。我唯一想要的是在编辑文本中,如果我输入任何单词,它应该过滤列表视图项。 我已经搜索过并尝试过,但没有成功。 现在我指的是这个教程: http://www.androidbegin.com/tutorial/android-search-filter-listview-images-and-texts-tutorial/

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

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

【问题讨论】:

标签: android listview baseadapter


【解决方案1】:

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

现在你去吧,这将正常工作。!希望如此

我做了同样的事情,这是我的代码!

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

}

并在 Main 中调用它!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    相关资源
    最近更新 更多