简体   繁体   English

搜索的项目在点击时给我错误的结果

[英]Searched Items giving me wrong results on click

I have a list of China, USA, Canada, and Japan with a search bar on the action bar.我有一个中国、美国、加拿大和日本的列表,操作栏上有一个搜索栏。 Everything works well but when I click on the searched item it gives me a wrong result.一切正常,但是当我点击搜索的项目时,它给了我一个错误的结果。 For example, if I search for Canada I see the result for Canada but when I click on it, it shows me the details for China.例如,如果我搜索加拿大,我会看到加拿大的结果,但是当我单击它时,它会显示中国的详细信息。 What can I do for correct result on click?点击时我能做些什么来获得正确的结果? Below are the screenshot and code:-以下是屏幕截图和代码:-

在此处输入图像描述

package com.example.search;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


    ListView listView;
    String[] nameList =

            {
                    "China",
                    "USA",
                    "Canada",
                    "Japan",
            };

    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        listView = (ListView) findViewById(R.id.list);

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, nameList);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                if (position == 0)  {
                    Intent intent0 = new Intent(getBaseContext(), China.class);
                    startActivity(intent0);
                }
                if (position == 1) {
                    Intent intent1 = new Intent(getBaseContext(), USA.class);
                    startActivity(intent1);
                }
                if (position == 2) {
                    Intent intent2 = new Intent(getBaseContext(), Canada.class);
                    startActivity(intent2);
                }
                if (position == 3) {
                    Intent intent3 = new Intent(getBaseContext(), Japan.class);
                    startActivity(intent3);
                }
                {

                }


            }

        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_search_menu, menu);
        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setQueryHint("Search here!");

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;

            }

            @Override
            public boolean onQueryTextChange(String newText) {
                MainActivity.this.adapter.getFilter().filter(newText);
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }
}


 

I see in your override onItemClick method you are starting different activity with respect to position, but your code will work only for your original item list that is {"China","USA","Canada","Japan"};我在您的覆盖 onItemClick 方法中看到您正在启动与 position 相关的不同活动,但您的代码仅适用于您的原始项目列表 {"China","USA","Canada","Japan"};

so what happens after you apply filter, your list in your adapter gets updated and so is your item positioning.所以在你应用过滤器之后会发生什么,你的适配器中的列表会更新,你的项目定位也会更新。 following your example after user searched for "Canada" the list in adapter get updated to {"Canada"}.在用户搜索“加拿大”后,按照您的示例,适配器中的列表将更新为 {“Canada”}。 now as you can see at position 0 instead of "China" we have "Canada" now现在你可以看到 position 0 而不是“中国”我们现在有“加拿大”

Conclusion: When doing filtering we can never rely on positions of items as they can change.结论:在进行过滤时,我们永远不能依赖项目的位置,因为它们可以改变。 but what we can do is get current item at that position from adapter and match its data但我们可以做的是从适配器获取 position 的当前项目并匹配其数据

Here you can Replace listView.setOnItemClickListener with the following code在这里你可以用下面的代码替换 listView.setOnItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        
            String country = (listView.getItemAtPosition(position));
     
            if (country.equals("China"))  {
                Intent intent0 = new Intent(getBaseContext(), China.class);
                startActivity(intent0);
            }
            if (country.equals("USA"))  {
                Intent intent1 = new Intent(getBaseContext(), USA.class);
                startActivity(intent1);
            }
            if (country.equals("Canada"))  {
                Intent intent2 = new Intent(getBaseContext(), Canada.class);
                startActivity(intent2);
            }
            if (country.equals("Japan"))  {
                Intent intent3 = new Intent(getBaseContext(), Japan.class);
                startActivity(intent3);
            }
   }
});

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

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