gpt4 book ai didi

java - 尝试在 Recyclerview 中实现 ONClickListener 时出错

转载 作者:行者123 更新时间:2023-12-02 05:13:01 27 4
gpt4 key购买 nike

我对尝试在 DialogFragment 的适配器中实现 ClickListener 感到很困惑。

我在代码行上收到错误:

holder.bind(tzmodel.get(position), listener);

错误提示(ViewHolder 中的bind() 无法应用)。似乎与 ViewHolder 代码不匹配。

有人可以帮忙吗?

我的 timeZoneAdapter 的代码:

public class timeZoneAdapter extends RecyclerView.Adapter<timeZoneAdapter.ViewHolder> {

public interface OnItemClickListener {

void onItemClick(TimezoneModel item);
}

private Context c;
private List<TimezoneModel> tzmodel;
private final OnItemClickListener listener;

public timeZoneAdapter(Context c, List<TimezoneModel> tzModel, OnItemClickListener listener) {

this.c = c;
this.tzmodel = tzModel;
this.listener = listener;
}

//INITIALIE TZ
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.timezonemodel, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

holder.bind(tzmodel.get(position), listener); <-- error
}

@Override
public int getItemCount() {

return tzmodel.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

TextView timeTxt, currenttimeTxt, regionTxt;

public ViewHolder(View itemView) {

super(itemView);
timeTxt= (TextView) itemView.findViewById(R.id.timeTxt);
currenttimeTxt= (TextView) itemView.findViewById(R.id.currentTimeTxt);
regionTxt= (TextView) itemView.findViewById(R.id.regionTxt);
}

public void bind(ViewHolder viewHolder, int i, final OnItemClickListener listener) {

viewHolder.timeTxt.setText(tzmodel.get(i).getTime());
viewHolder.currenttimeTxt.setText(tzmodel.get(i).getCurrentTime());
viewHolder.regionTxt.setText(tzmodel.get(i).getRegion());

itemView.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) {

listener.onItemClick((TimezoneModel) tzmodel);
}
});
}
}
}

最佳答案

问题在于您放入定义的绑定(bind)方法中的参数。

您可以通过以下方式在 ViewHolder 类中定义公共(public)方法 bind:

public void bind(ViewHolder viewHolder, int i, final OnItemClickListener listener){

//...

}

您可以修复编译错误,将调用更改为:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bind(holder,position, listener);
}

我还与您分享了一些更正,因为如果您在 ViewHolder 类中创建公共(public)方法,则不需要编写 viewHolder 实例,因为您可以使用此运算符进行访问:


public class TimeZoneAdapter extends RecyclerView.Adapter<TimeZoneAdapter.ViewHolder> {

public interface OnItemClickListener {
void onItemClick(TimezoneModel item);
}

private Context c;
private List<TimezoneModel> tzmodel;
private final OnItemClickListener listener;

public TimeZoneAdapter(Context c, List<TimezoneModel> tzModel, OnItemClickListener listener) {
this.c = c;
this.tzmodel = tzModel;
this.listener = listener;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bind(tzmodel.get(position), listener);
}

@Override
public int getItemCount() {

return tzmodel.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

TextView timeTxt, currenttimeTxt, regionTxt;

public ViewHolder(View itemView) {

super(itemView);
timeTxt= itemView.findViewById(R.id.timeTxt);
currenttimeTxt= itemView.findViewById(R.id.currentTimeTxt);
regionTxt= itemView.findViewById(R.id.regionTxt);
}

public void bind(TimezoneModel item , final OnItemClickListener listener) {

this.timeTxt.setText(item.getTime());
this.currenttimeTxt.setText(item.getCurrentTime());
this.regionTxt.setText(item.getRegion());

itemView.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) {
listener.onItemClick((TimezoneModel) tzmodel);
}
});
}
}
}


关于java - 尝试在 Recyclerview 中实现 ONClickListener 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56314934/

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