BaseAdapter Listview过滤Android
Posted
技术标签:
【中文标题】BaseAdapter Listview过滤Android【英文标题】:BaseAdapter Listview Filtering Android 【发布时间】:2011-10-17 14:22:34 【问题描述】:在我的Listview
中,我有文本和图像,它们存储在ArrayList
。
我正在使用BaseAdapter
来显示列表。
我想根据在EditText
中键入的文本在列表中使用filter
,但不起作用。
我的代码是:adv is EditText
adv.addTextChangedListener(new TextWatcher()
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3)
// TODO Auto-generated method stub
viewadapter.getFilter().filter(s);
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3)
// TODO Auto-generated method stub
@Override
public void afterTextChanged(Editable arg0)
// TODO Auto-generated method stub
);
而我的BaseAdapter
实现了Filterable
所以getFilter
方法是:
@Override
public Filter getFilter()
// TODO Auto-generated method stub
if (mFilter == null)
mFilter = new ArrayFilter();
return mFilter;
而我的ArrayFilter
是:
private class ArrayFilter extends Filter
@Override
protected FilterResults performFiltering(CharSequence prefix)
FilterResults results = new FilterResults();
if (mNames == null)
synchronized (mLock)
mNames = new ArrayList<String>(mObjects);
if (prefix == null || prefix.length() == 0)
synchronized (mLock)
ArrayList<String> list = new ArrayList<String>(mNames);
results.values = list;
results.count = list.size();
else
String prefixString = prefix.toString().toLowerCase();
final ArrayList<String> values = mNames;
final int count = values.size();
final ArrayList<String> newValues = new ArrayList<String>(count);
for (int i = 0; i < count; i++)
final String value = values.get(i);
final String valueText = value.toString().toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString))
newValues.add(value);
else
final String[] words = valueText.split(" ");
final int wordCount = words.length;
for (int k = 0; k < wordCount; k++)
if (words[k].startsWith(prefixString))
newValues.add(value);
break;
results.values = newValues;
results.count = newValues.size();
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
//noinspection unchecked
mObjects = (List<String>) results.values;
if (results.count > 0)
viewadapter.notifyDataSetChanged();
else
viewadapter.notifyDataSetInvalidated();
因为mNames
是ArrayList
,我想用它过滤ListView
。
还启用了setTextFilterEnabled(true).
,但过滤不起作用。
这是正确的方法吗?有什么问题?如果没有,该怎么做?
【问题讨论】:
您解决了这个问题吗?您能给我们反馈吗? 不,我没有得到解决,那就是我保持打开状态!如果你有,请在这里发帖谢谢 如何在 ArrayList这是 API 演示示例
public class SearchViewFilterMode extends Activity implements SearchView.OnQueryTextListener
private static final String TAG = "SearchViewFilterMode";
private SearchView mSearchView;
private ListView mListView;
private ArrayAdapter<String> mAdapter;
private final String[] mStrings = Cheeses.sCheeseStrings;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.searchview_filter);
mSearchView = (SearchView) findViewById(R.id.search_view);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setAdapter(mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
mStrings));
mListView.setTextFilterEnabled(true);
setupSearchView();
private void setupSearchView()
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(false);
mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
public boolean onQueryTextChange(String newText)
if (TextUtils.isEmpty(newText))
mListView.clearTextFilter();
else
mListView.setFilterText(newText.toString());
return true;
public boolean onQueryTextSubmit(String query)
return false;
【讨论】:
以上是关于BaseAdapter Listview过滤Android的主要内容,如果未能解决你的问题,请参考以下文章
使用 Baseadapter 过滤 ListView 过滤文本而不是图像
ListView BaseAdapter 中的 EditText 奇怪行为
带有 BaseAdapter 的 Android ListView 内的 ImageButton