onListItemClick() 不适用于 ArrayAdapter 中的过滤搜索列表 (Android)
onListItemClick() not working on filtered search list in an ArrayAdapter (Android)
我有一个列表视图,我在其中实施了 onListItemClick()
。它运行良好,但是当我使用搜索过滤列表时,它会打开错误的列表项(有时甚至在过滤列表中都看不到)。下面是我的代码,不确定我遗漏了什么或做错了什么。请查看内联注释以了解代码的作用。任何帮助将不胜感激。
private static final int EDITOR_ACTIVITY_REQUEST = 2001;
private DataSource mSource;
private ArrayAdapter<SampleData> mAdapter;
private EditText mSearchField;
private List<SampleData> mDataList;
private SampleData mData;
private Intent mIntent;
// done in onCreate()
mSource = new DataSource(this);
updateDisplay();
// set up in onCreateOptionsMenu()
mSearchField = (EditText) menu.findItem(R.id.action_search).getActionView();
mSearchField.addTextChangedListener(searchTextWatcher);
// method to refresh display
private void updateDisplay() {
// fetch data from the database and display in list view
mDataList = mSource.retrieveAllData();
mAdapter = new ArrayAdapter<>(this, R.layout.list_item_layout, mDataList);
setListAdapter(mAdapter);
}
// respond to list item clicks
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// view data of an item pressed on
editData(position);
}
// edit the data
protected void editData(int position) {
mData = mDataList.get(position);
mIntent = new Intent(this, DataEditor.class);
mIntent.putExtra(Constants.KEY, mData.getKey());
mIntent.putExtra(Constants.TEXT, mData.getText());
startActivityForResult(mIntent, EDITOR_ACTIVITY_REQUEST);
}
// Search TextWatcher
private TextWatcher searchTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String searchText = mSearchField.getText().toString()
.toLowerCase(Locale.getDefault());
mAdapter.getFilter().filter(searchText);
// tried these but never worked
/**
* mAdapter.notifyDataSetInvalidated();
* mAdapter.notifyDataSetChanged();
* getListView().setAdapter(mAdapter);
*/
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
};
而不是 mDataList.get(position)
,使用 mAdapter.getItem(position)
。 position
基于当前列表内容,mDataList
是未过滤的原始列表内容。
我有一个列表视图,我在其中实施了 onListItemClick()
。它运行良好,但是当我使用搜索过滤列表时,它会打开错误的列表项(有时甚至在过滤列表中都看不到)。下面是我的代码,不确定我遗漏了什么或做错了什么。请查看内联注释以了解代码的作用。任何帮助将不胜感激。
private static final int EDITOR_ACTIVITY_REQUEST = 2001;
private DataSource mSource;
private ArrayAdapter<SampleData> mAdapter;
private EditText mSearchField;
private List<SampleData> mDataList;
private SampleData mData;
private Intent mIntent;
// done in onCreate()
mSource = new DataSource(this);
updateDisplay();
// set up in onCreateOptionsMenu()
mSearchField = (EditText) menu.findItem(R.id.action_search).getActionView();
mSearchField.addTextChangedListener(searchTextWatcher);
// method to refresh display
private void updateDisplay() {
// fetch data from the database and display in list view
mDataList = mSource.retrieveAllData();
mAdapter = new ArrayAdapter<>(this, R.layout.list_item_layout, mDataList);
setListAdapter(mAdapter);
}
// respond to list item clicks
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// view data of an item pressed on
editData(position);
}
// edit the data
protected void editData(int position) {
mData = mDataList.get(position);
mIntent = new Intent(this, DataEditor.class);
mIntent.putExtra(Constants.KEY, mData.getKey());
mIntent.putExtra(Constants.TEXT, mData.getText());
startActivityForResult(mIntent, EDITOR_ACTIVITY_REQUEST);
}
// Search TextWatcher
private TextWatcher searchTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String searchText = mSearchField.getText().toString()
.toLowerCase(Locale.getDefault());
mAdapter.getFilter().filter(searchText);
// tried these but never worked
/**
* mAdapter.notifyDataSetInvalidated();
* mAdapter.notifyDataSetChanged();
* getListView().setAdapter(mAdapter);
*/
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
};
而不是 mDataList.get(position)
,使用 mAdapter.getItem(position)
。 position
基于当前列表内容,mDataList
是未过滤的原始列表内容。