Search code examples
androidsearchandroid-listviewandroid-arrayadaptertextwatcher

onListItemClick() not working on filtered search list in an ArrayAdapter (Android)


I have a list view on which I have implemented an onListItemClick(). It works well but when I filter the list using search, it opens wrong list items (sometimes ones not even seen in the filtered list). Below is my code, not sure what I am missing or doing wrongly. Please see inline comments to get what the code does. Any help will be much appreciated.

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) {

        }

    };

Solution

  • Instead of mDataList.get(position), use mAdapter.getItem(position). The position is based on the current list contents, and mDataList is the raw unfiltered list contents.