在我的Listview
中,我有文字和图片,并存储在ArrayList
。
我正在使用BaseAdapter
来显示列表。
我想根据filter
中输入的文字在列表中使用EditText
,但无效。
我的代码是:adv是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
的{{1}}。
并且还启用了ListView
,但过滤无效。
这是正确的做法吗?有什么不对吗?如果没有,该怎么做?
答案 0 :(得分:0)
这是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;
}
}