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

时间:2017-03-10 13:57:48

标签: android listview baseadapter

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

我想通过适配器中附加的textview过滤它。我能够在列表视图(图像+文本)中正确列出项目。我想要的只是在编辑文本中,如果我输入任何单词,它应该过滤列表视图项。 我搜索过同样的但尝试过,但没有成功。 现在我指的是这个教程: 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();
    }
    

1 个答案:

答案 0 :(得分:0)

Search View和Edittext都可用于此过滤器,但搜索视图更好。!

现在你走了,这样可以正常工作。希望如此

我在这里做了同样的事情是我的代码。!

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