SearchView使用列表视图检索错误的数据

时间:2020-07-29 16:54:00

标签: android-studio

搜索项目时,我可以检索相应歌曲的TitleArtistImage。但是,在ListView中显示的前2个项目始终会返回列表中的最后一首歌曲,而不是显示的正确歌曲。

例如,当点击“生活是美好的”或“地狱般的美好”时,将显示歌曲“ First Time”。但是,“ Perfect”,“ Lemon”和“ Autopilot”工作正常。这些是用于搜索的唯一代码! SearchByTitleGetTitle()是从其中硬编码了歌曲的文件中提取的!

我的活动:

    ListView listView;
    ArrayList<String> stringArrayList = new ArrayList<>();
    ArrayAdapter<String> adapter;
    private SongCollection songCollection = new SongCollection();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
    
        listView = findViewById(R.id.list_view);
    

        stringArrayList.add("Life is Good");
        stringArrayList.add("Good As Hell");
        stringArrayList.add("Perfect");
        stringArrayList.add("Lemon");
        stringArrayList.add("Autopilot");
        stringArrayList.add("First Time");
    
        adapter = new ArrayAdapter<>(SearchActivity.this, android.R.layout.simple_list_item_1, stringArrayList);
    
        listView.setAdapter(adapter);
    
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),
                        adapter.getItem(i), Toast.LENGTH_SHORT).show();
            }
        });
    
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String value = listView.getItemAtPosition(i).toString();
                Song selectedSong = songCollection.searchByTitle(value);
                AppUtil.popMessage(view.getContext(), "Streaming song: " + selectedSong.getTitle());
                sendDataToActivity(selectedSong);
            }
        });
 }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();

        menuInflater.inflate(R.menu.my_menu, menu);

        MenuItem menuItem = menu.findItem(R.id.action_search);

        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);

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

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);

                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }
}

我活动的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#404040"
    tools:context=".SearchActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#404040">

        <ListView
            android:id="@+id/list_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#FFFFFF"
            android:divider="@color/white"
            android:dividerHeight="2dp"
            tools:listitem="@android:layout/simple_list_item_1">
        </ListView>
    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

发生这种情况是因为您需要创建一个包含搜索结果的新列表,而不必对原始列表stringArrayList

进行搜索操作

在这种情况下,请创建一个新列表mylist,然后存储搜索结果并在onQueryTextChange中调用该列表

创建此方法,我根据您的代码修改了代码

private void search(String str) {
        ArrayList<String> myList = new ArrayList<>();
        for (Quote object : stringArrayList) {
            if (object.GetTitle().toLowerCase().contains(str.toLowerCase())) {
                myList.add(object);
            }
        }
        AdapterClass adapterClass = new AdapterClass(this, myList);//, swiper
        recyclerView.setAdapter(adapterClass);
    }

现在,像这样

在搜索setOnQueryTextListener下调用此方法
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    search(newText);
                    return true;
                }
            });

可选 您可以做的另一种修改是,只要其中有一些文本,您就可以检查setOnQueryTextListener

if (searchView != null) {
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    search(s);
                    return true;
                }
            });
        }

如果我的答案可以解决您的问题,则可以单击我的答案旁边的灰色对勾按钮,对我进行投票,将我的答案标记为正确的答案。