【发布时间】:2017-03-10 13:57:47
【问题描述】:
我的动机是从包含图像和文本视图的列表视图项目中过滤和显示用户在编辑文本视图中输入的项目。但我无法正确过滤(我猜实施不正确 + Android 新手)
我想通过适配器中附加的文本视图对其进行过滤。我能够在列表视图(图像+文本)中正确列出项目。我唯一想要的是在编辑文本中,如果我输入任何单词,它应该过滤列表视图项。 我已经搜索过并尝试过,但没有成功。 现在我指的是这个教程: http://www.androidbegin.com/tutorial/android-search-filter-listview-images-and-texts-tutorial/
任何帮助将不胜感激。 谢谢
-
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); } }); } -
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(); }
【问题讨论】:
-
创建一个模型类而不是创建该模型类的列表,而不是创建单独的列表。这是您要求的完美解决方案。 androidbegin.com/tutorial/android-search-listview-using-filter
标签: android listview baseadapter