Search code examples
javaandroidsearchview

Android Searchview filtering wrong on item click with ListView


I have tried other solutions. But the problem is different for me. I have implemented a SearchView with ListView in my app. When I search for something the first item of the array is opened on item click. But I need to open the same item which is listed on the search. The search result is filtering well, but onClick item is giving different item. Please note that my question is different and not a duplicate. My code is below:


public class Searchpage extends AppCompatActivity {
    SearchView searchView;
    ListView listView;
    ArrayAdapter<String> adapter;


    String[] name = {
           "General Science", "Social Science", "Maths", "English", "Politics", "IT"
  };
    String[] file= {
           "science.pdf", "social.pdf", "maths.pdf", "english.pdf", "politics.pdf", it.pdf"
    };
    int[] pagenum = {
            0,0,0,0,0,0
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchpage);
        searchView = findViewById(R.id.srch);
        listView = findViewById(R.id.searchlist);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
        listView.setAdapter(adapter);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                adapter.getFilter().filter(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent ij = new Intent(getApplicationContext(), PdfView.class);
                ij.putExtra("furl", "fl/" + pagenum[position]);
                ij.putExtra("pagenm", pagenum[position]);
                startActivity(ij);
            }
        });
    }


}


Solution

  • First of all use ArrayList instead of array to initialize ArrayAdapter

    ArrayList<String> name = new ArrayList<>(Arrays.asList(
            "General Science", "Social Science", "Maths", "English", "Politics", "IT"
    ));
    

    Then use below code to find out correct position of your filtered data

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
        int actualPosition = name.indexOf(adapter.getItem(position));
    
        Intent ij = new Intent(getApplicationContext(), PdfView.class);
        ij.putExtra("furl", "fl/" + file[actualPosition]);
        ij.putExtra("pagenm", pagenum[actualPosition]);
        startActivity(ij);
    }