我正在关注RecyclerView示例,我有这个错误
incompatible types: cannot cast viewholder to imageviewholder
关于此代码
case TYPE_IMAGE:
ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
这是我的完整代码(已编辑)
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<String> mDataset;
public class ImageViewHolder extends RecyclerView.ViewHolder {
//ImageView mImage;
public TextView txtHeader;
public TextView txtFooter;
public ImageViewHolder(View itemView) {
super (itemView);
txtHeader = (TextView) itemView.findViewById(R.id.firstLine1);
txtFooter = (TextView) itemView.findViewById(R.id.secondLine1);
}
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class TextViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView txtHeader;
public TextView txtFooter;
public TextViewHolder(View v) {
super(v);
txtHeader = (TextView) v.findViewById(R.id.firstLine);
txtFooter = (TextView) v.findViewById(R.id.secondLine);
}
}
public void add(int position, String item) {
mDataset.add(position, item);
notifyItemInserted(position);
}
public void remove(String item) {
int position = mDataset.indexOf(item);
mDataset.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<String> myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
TextViewHolder vh = new TextViewHolder(v);
return vh;
}
private static final int TYPE_IMAGE = 1;
private static final int TYPE_GROUP = 2;
@Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position == 0 ? TYPE_IMAGE : TYPE_GROUP;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {
switch (TextViewHolder.getItemViewType()) {
case TYPE_IMAGE:
ImageViewHolder imageViewHolder = (ImageViewHolder) TextViewHolder;
// imageViewHolder.mImage.setImageResource(...);
final String namev = mDataset.get(position);
imageViewHolder.txtHeader.setText(mDataset.get(position));
imageViewHolder.txtHeader.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
remove(namev);
}
});
break;
case TYPE_GROUP:
TextViewHolder viewHolder = (TextViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final String name = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
viewHolder.txtHeader.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});
viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));
break;
}
/*
// - get element from your dataset at this position
// - replace the contents of the view with that element
final String name = mDataset.get(position);
holder.txtHeader.setText(mDataset.get(position));
holder.txtHeader.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});
holder.txtFooter.setText("Footer: " + mDataset.get(position));
*/
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}
错误
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
switch (viewType) {
case TYPE_IMAGE: {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// return new ImageViewHolder(itemView);
ImageViewHolder vh = new ImageViewHolder(itemView);
return vh;
}
case TYPE_GROUP: {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// return new TextViewHolder(itemView);
TextViewHolder vh = new TextViewHolder(itemView);
return vh;
}
}
}// error here missing return statement
答案 0 :(得分:4)
你应该改变:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.TextViewHolder>
有:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
然后更改onCreateViewHolder以接受多个ViewTypes:
@Override
public MyAdapter.TextViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)
有:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)
然后更改onBindViewHolder方法:
@Override
public void onBindViewHolder(TextViewHolder TextViewHolder, int position)
有:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
不要忘记设置ViewHolder类型:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType){
switch (viewType) {
case TYPE_IMAGE: {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.XXX, parent, false);
return new ImageViewHolder(itemView);
}
case TYPE_GROUP: {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.XXX, parent, false);
return new TextViewHolder(itemView);
}
}
答案 1 :(得分:4)
这是更新文件:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<String> mDataset;
public class ImageViewHolder extends RecyclerView.ViewHolder {
//ImageView mImage;
public TextView txtHeader;
public TextView txtFooter;
public ImageViewHolder(View itemView) {
super (itemView);
txtHeader = (TextView) itemView.findViewById(R.id.firstLine1);
txtFooter = (TextView) itemView.findViewById(R.id.secondLine1);
}
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class TextViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView txtHeader;
public TextView txtFooter;
public TextViewHolder(View v) {
super(v);
txtHeader = (TextView) v.findViewById(R.id.firstLine);
txtFooter = (TextView) v.findViewById(R.id.secondLine);
}
}
public void add(int position, String item) {
mDataset.add(position, item);
notifyItemInserted(position);
}
public void remove(String item) {
int position = mDataset.indexOf(item);
mDataset.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<String> myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
if (viewType == TYPE_IMAGE) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
ImageViewHolder vh = new ImageViewHolder(v);
return vh;
} else {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
TextViewHolder vh = new TextViewHolder(v);
return vh;
}
}
private static final int TYPE_IMAGE = 1;
private static final int TYPE_GROUP = 2;
@Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position == 0 ? TYPE_IMAGE : TYPE_GROUP;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {
switch (TextViewHolder.getItemViewType()) {
case TYPE_IMAGE:
ImageViewHolder imageViewHolder = (ImageViewHolder) TextViewHolder;
// imageViewHolder.mImage.setImageResource(...);
final String namev = mDataset.get(position);
imageViewHolder.txtHeader.setText(mDataset.get(position));
imageViewHolder.txtHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(namev);
}
});
break;
case TYPE_GROUP:
TextViewHolder viewHolder = (TextViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final String name = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
viewHolder.txtHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});
viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));
break;
}
/*
// - get element from your dataset at this position
// - replace the contents of the view with that element
final String name = mDataset.get(position);
holder.txtHeader.setText(mDataset.get(position));
holder.txtHeader.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});
holder.txtFooter.setText("Footer: " + mDataset.get(position));
*/
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}