RecyclerView rvList = findViewById(R.id.rv_list); rvList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(RecyclerView recyclerView, View view, int i, long l) { ShowData item = (ShowData) recyclerView.getAdapter().getItemId(i); if (!isStarted) { modeDialog(item); } else { CustomToast.showLoad(MainActivity.this, getString(R.string.please_stop_read), 2000); } return true; } });Unable to start activity ComponentInfo{com.ives.upcrfid_zebra/com.ives.upcrfid_zebra.MainActivity}: java.lang.ClassCastException: androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.ListView
时间: 2023-11-27 13:53:00 浏览: 80
这段代码中出现了类型转换异常,原因是在代码中将 RecyclerView 对象强制转换成了 ListView 对象。解决方法是修改代码,将 RecyclerView 和对应的 Adapter 类型进行匹配,例如:
```
RecyclerView rvList = findViewById(R.id.rv_list);
rvList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(RecyclerView recyclerView, View view, int i, long l) {
ShowDataAdapter adapter = (ShowDataAdapter) recyclerView.getAdapter();
ShowData item = adapter.getItem(i);
if (!isStarted) {
modeDialog(item);
} else {
CustomToast.showLoad(MainActivity.this, getString(R.string.please_stop_read), 2000);
}
return true;
}
});
```
其中 `ShowDataAdapter` 是你自定义的 RecyclerView Adapter 类型,`ShowData` 是 Adapter 中数据项的类型。
阅读全文