gpt4 book ai didi

android - 添加多个回收 View 时,不可转换类型无法将 dataobjectholder 转换为 groupview 持有者

转载 作者:行者123 更新时间:2023-11-30 01:36:55 25 4
gpt4 key购买 nike

好吧,我是 recyclerview 的新手,我在这里迷路了。我想在一个 Activity 中有 2 个(或更多)recyclerview。我的问题出在这部分代码中

    error: here inconvertable type cannot cast dataobjectholder to groupview holder

case 2:
GroupViewHolder groupviewHolder = (GroupViewHolder) holder; //here inconvertable type cannot cast dataobjectholder to groupview holder
groupviewHolder.label1.setText(mDataset.get(position).getmText1());
// holder.dateTime.setText(mDataset.get(position).getmText2());
// holder.imageView.setImageResource();
Picasso.
with(mContext).
load("http://justedhak.com/funnyimages/uploaded_images563cd66b98a20.png")
.placeholder(R.drawable.error)
.fit()
.noFade()
.into(groupviewHolder.imageView1);
break;
}

这是我的完整代码

public class MyRecyclerViewAdapter extends RecyclerView
.Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
Context mContext;
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
public MyRecyclerViewAdapter(Context context)
{

this.mContext =context;
}

)
public static class GroupViewHolder extends RecyclerView.ViewHolder
{
TextView label1;
ImageView imageView1;

// TextView dateTime;

public GroupViewHolder(View itemView) {
super(itemView);
label1 = (TextView) itemView.findViewById(R.id.textView);
// dateTime = (TextView) itemView.findViewById(R.id.textView2);
imageView1 = (ImageView) itemView.findViewById(R.id.image);

// itemView.setOnClickListener(this);
}
}
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView label;
ImageView imageView;

// TextView dateTime;

public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
// dateTime = (TextView) itemView.findViewById(R.id.textView2);
imageView = (ImageView) itemView.findViewById(R.id.image);

itemView.setOnClickListener(this);
}



@Override
public void onClick(View v) {
myClickListener.onItemClick(getPosition(), v);
}
}

public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}

public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
mDataset = myDataset;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item, parent, false);

DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
@Override
public int getItemViewType(int position)
{
return position==0? 1:2;
}
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
switch(holder.getItemViewType())
{
case 1:
holder.label.setText(mDataset.get(position).getmText1());
// holder.dateTime.setText(mDataset.get(position).getmText2());
// holder.imageView.setImageResource();
Picasso.
with(mContext).
load("http://justedhak.com/funnyimages/uploaded_images563cd66b98a20.png")
.placeholder(R.drawable.error)
.fit()
.noFade()
.into(holder.imageView);
break;

case 2:
GroupViewHolder groupviewHolder = (GroupViewHolder) holder;
groupviewHolder.label1.setText(mDataset.get(position).getmText1());
// holder.dateTime.setText(mDataset.get(position).getmText2());
// holder.imageView.setImageResource();
Picasso.
with(mContext).
load("http://justedhak.com/funnyimages/uploaded_images563cd66b98a20.png")
.placeholder(R.drawable.error)
.fit()
.noFade()
.into(groupviewHolder.imageView1);
break;
}

}

public void addItem(DataObject dataObj, int index) {
mDataset.add(dataObj);
notifyItemInserted(index);
}

public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}

@Override
public int getItemCount() {
return mDataset.size();
}

public interface MyClickListener {
public void onItemClick(int position, View v);
}

最佳答案

首先,你有两个不同的Viewholder,所以你的类extends RecyclerView.Adapter而不是 extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder>

现在看看你的onCreateViewHolder功能:它只返回一种类型的Viewholder。应该是:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
if (viewType == 1) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item, parent, false);

DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
} else {
//TODO: Inflate view and return GroupViewHolder
}
}

那么你的 onBindViewHolder 有错误功能:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch(getItemViewType(position)) {
// TODO...
}
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof DataObjectHolder) {
// Do this...
} else if (holder instanceof GroupViewHolder) {
// ...do that
}
}

关于android - 添加多个回收 View 时,不可转换类型无法将 dataobjectholder 转换为 groupview 持有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34999025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com