Search code examples
javaandroidsearchview

v7 searchview overlapping issue


example to what kind of problem I am running into.

Let say I have 6 elements in a listview all in order from left to right [1,2,3,51,4,5].

When I search for 5 it displays "51" and "5". When I click on "51" it opens up "1". The problem is that the filtered list overlaps the listview that displays all the elements. I was wondering how I can fix this issue. I am using v7.widget.SearchView

    @Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.recipe_menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.search_recipe).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (TextUtils.isEmpty(newText)) {
                lv.clearTextFilter();
            } else {
                lv.setFilterText(newText.toString());
            }
            return true;
        }
    });
    super.onCreateOptionsMenu(menu, inflater);
}

Solution

  • I think that you make mistake in onItemClick(AdapterView<?> parent, View view, int position, long id) method. You take data by position from data list. The "position" is a position of clicked item on list. So if you filtered your list and click on first item the value of position will be "1". You have to take value from adapter which is corresponding to clicked position. You should take item from adapter like is shown in code below:

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ...
    
            String data = ((ExerciseDAO)parent.getAdapter().getItem(position)).getTitle() ;
    
            ...
        }
    });
    

    If you have questions - just ask.