简体   繁体   English

使用 ListView 单击项目时,Android Searchview 过滤错误

[英]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.我在我的应用程序中实现了一个带有ListViewSearchView When I search for something the first item of the array is opened on item click .当我搜索某项时,数组的第一项在 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.搜索结果过滤得很好,但 onClick 项目给出了不同的项目。 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);
            }
        });
    }


}

First of all use ArrayList instead of array to initialize ArrayAdapter首先使用ArrayList代替array来初始化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然后使用下面的代码找出过滤数据的正确position

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM