谁能解决这个 ListView onItemClickListener 错误?在 searchView 中也出现错误

Can anyone solve this ListView onItemClickListener Error? Also getting bug in searchView

ItemModel.java

package com.example.poptunemusicplayer;

import java.io.Serializable;

public class ItemModel implements Serializable {
    private String name;

    public ItemModel(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

由于 String songName = (String) listView.getItemAtPosition(i); 在 listView.setOnItemClickListener 出现错误;

MainActivity.java

中的代码
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String songName = (String) listView.getItemAtPosition(i);
                startActivity(new Intent(getApplicationContext(), PlayerActivity.class)
                        .putExtra("songs", mySongs)
                        .putExtra("songname", songName)
                        .putExtra("position", i));
            }
        });

尝试在我的应用程序的 listView 中设置 onClick 但出现此错误

错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.poptunemusicplayer, PID: 7781
    java.lang.ClassCastException: com.example.poptunemusicplayer.ItemModel cannot be cast to java.lang.String
        at com.example.poptunemusicplayer.MainActivity.onItemClick(MainActivity.java:112)
        at android.widget.AdapterView.performItemClick(AdapterView.java:350)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1674)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:4085)
        at android.widget.AbsListView.run(AbsListView.java:6573)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7025)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

我也实现了 serachView,但是在我在上面写了一些东西后得到了错误的结果,并且在 onClick 上也得到了一些不同的结果。例如;

假设我的列表中有一些项目,例如:

如果我输入搜索“你”,它会给我结果:

它没有向我显示“年轻”,如果我单击“像你一样爱我”,它会向我显示“年轻”的数据。
既没有获取正确的数据,也没有获取正确的位置。

CustomAdapter 中的 getFilter() 方法

@Override
        public Filter getFilter(){
            Filter filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint == null | constraint.length() == 0){
                        filterResults.count = itemsModelsl.size();
                        filterResults.values = itemsModelsl;
                    }
                    else {
                        List<ItemModel> resultModel = new ArrayList<>();
                        String searchStr = constraint.toString().toLowerCase();
                        for (ItemModel itemModel: itemsModelsl){
                            if (itemModel.getName().contains(searchStr)){
                                resultModel.add(itemModel);
                            }
                            filterResults.count = resultModel.size();
                            filterResults.values = resultModel;
                        }
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    itemsModelListFiltered = (List<ItemModel>) results.values;
                    notifyDataSetChanged();
                }
            };
            return filter;
        }

OptionsMenu() 方法中的 SearchView 代码

case R.id.searchid:

                SearchView searchView = (SearchView) item.getActionView();
                SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
                searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
                searchView.setQueryHint("Type here to search");

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

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        customAdapter.getFilter().filter(newText);
                        return true;

                    }
                });

                return true;

我错过了哪一步?

从外观上看,您的 ListView 适配器包含 class ItemModel 的实例,而不仅仅是简单的 Strings.

java.lang.ClassCastException: com.example.poptunemusicplayer.ItemModel cannot be cast to java.lang.String

要解决此问题,请更改此

String songName = (String) listView.getItemAtPosition(i);

ItemModel itemModel = (ItemModel) listView.getItemAtPosition(i);
String songName = itemModel.getName();

它应该不会再崩溃了。

我的建议是研究使用 RecyclerView,但这是另一天的话题。