RecyclerView 适配器给出错误 "The hierarchy of the type RecycleAdapter is inconsistent"
RecyclerView Adapter gives error "The hierarchy of the type RecycleAdapter is inconsistent"
我正在尝试将新的 RecyclerView
添加到我的 Fragement
项目中。我正在关注 Link1 上的视频教程。当我尝试为 RecyclerView
创建适配器时,我开始收到此错误。我正在使用 Eclipse 朱诺。我无法弄清楚问题所在。请帮忙
我的代码:
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {
private LayoutInflater inflator;
List <Item> data = Collections.EMPTY_LIST;
public RecycleAdapter(Context context, List<Item> data){
inflator = LayoutInflater.from(context);
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
View view = inflator.inflate(R.layout.row_staggered_new, parent, false);
MyViewHolder holder = MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Item current = data.get(position);
holder.text.setText(current.title);
holder.image.setImageResource(current.image);
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
public ImageView image;
public TextView text;
public MyViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.imageView1);
text = (TextView) itemView.findViewById(R.id.textview1);
}
}
}
MyViewHolder
和 RecycleAdapter
都给出错误 "The hierarchy of the type RecycleAdapter is inconsistent"
。
在 RecycleAdapter
的 extends 部分显示错误
"Bound mismatch: The type RecycleAdapter.MyViewHolder is not a valid substitute for the bounded parameter <VH extends RecyclerView.ViewHolder> of the type RecyclerView.Adapter<VH>"
。
有什么指点吗?
在onBindViewHolder
方法中不能将RecyclerView.ViewHolderclass直接转换为MyViewHolder
,因为后者是前者的扩展。
您需要显式执行转换,即
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
MyViewHolder holder = (MyViewHolder)viewHolder;
...
}
编辑
您有多个编译问题,但您提到的问题是因为您扩展了错误的 class 类型。 class 应该像这样扩展
extends RecyclerView.Adapter<RecyclerView.ViewHolder>
我发现了问题。问题是我使用的是旧版本和不包含 class android.support.v4.view.ScrollingView class 的 v4 支持库。我用新版本替换了旧的 v4 库,错误消失了!
I found the problem. The issue was that I was using an older version and v4 support library which does not contain the class android.support.v4.view.ScrollingView class. I replaced my old v4 library with the new version and the error was gone!
对我有用,补充回答:
Eclipse 将包含一个名为 "appcompat_v7" 的项目,我们也应该替换它的库。
另一种解决方案是..
- 从库中删除 jar 文件。
- 从 sdk 复制
android-support-v7-appcompat
到工作区 → extra → android → support-v7 → appcompat
- 您将在 appcompat 中找到 v4 库
- 右键单击 appcompat,如果不是,则将其标记为库
- 现在右键单击您的项目转到 java 构建路径将 appcompat 添加为库。
享受
我正在尝试将新的 RecyclerView
添加到我的 Fragement
项目中。我正在关注 Link1 上的视频教程。当我尝试为 RecyclerView
创建适配器时,我开始收到此错误。我正在使用 Eclipse 朱诺。我无法弄清楚问题所在。请帮忙
我的代码:
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {
private LayoutInflater inflator;
List <Item> data = Collections.EMPTY_LIST;
public RecycleAdapter(Context context, List<Item> data){
inflator = LayoutInflater.from(context);
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
View view = inflator.inflate(R.layout.row_staggered_new, parent, false);
MyViewHolder holder = MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Item current = data.get(position);
holder.text.setText(current.title);
holder.image.setImageResource(current.image);
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
public ImageView image;
public TextView text;
public MyViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.imageView1);
text = (TextView) itemView.findViewById(R.id.textview1);
}
}
}
MyViewHolder
和 RecycleAdapter
都给出错误 "The hierarchy of the type RecycleAdapter is inconsistent"
。
在 RecycleAdapter
的 extends 部分显示错误
"Bound mismatch: The type RecycleAdapter.MyViewHolder is not a valid substitute for the bounded parameter <VH extends RecyclerView.ViewHolder> of the type RecyclerView.Adapter<VH>"
。
有什么指点吗?
在onBindViewHolder
方法中不能将RecyclerView.ViewHolderclass直接转换为MyViewHolder
,因为后者是前者的扩展。
您需要显式执行转换,即
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
MyViewHolder holder = (MyViewHolder)viewHolder;
...
}
编辑
您有多个编译问题,但您提到的问题是因为您扩展了错误的 class 类型。 class 应该像这样扩展
extends RecyclerView.Adapter<RecyclerView.ViewHolder>
我发现了问题。问题是我使用的是旧版本和不包含 class android.support.v4.view.ScrollingView class 的 v4 支持库。我用新版本替换了旧的 v4 库,错误消失了!
I found the problem. The issue was that I was using an older version and v4 support library which does not contain the class android.support.v4.view.ScrollingView class. I replaced my old v4 library with the new version and the error was gone!
对我有用,补充回答:
Eclipse 将包含一个名为 "appcompat_v7" 的项目,我们也应该替换它的库。
另一种解决方案是..
- 从库中删除 jar 文件。
- 从 sdk 复制
android-support-v7-appcompat
到工作区 → extra → android → support-v7 → appcompat - 您将在 appcompat 中找到 v4 库
- 右键单击 appcompat,如果不是,则将其标记为库
- 现在右键单击您的项目转到 java 构建路径将 appcompat 添加为库。
享受