gpt4 book ai didi

java - 如何创建具有扩展另一个类并实现接口(interface)的类型参数的基本适配器?

转载 作者:行者123 更新时间:2023-11-29 02:59:06 25 4
gpt4 key购买 nike

我看到了 answer对于这个问题,

Java: how to create a generic class that requires a data type which extends a class and implements an interface?

所以我尝试为 recyclerview 创建一个基本适配器。这是基本适配器的完整代码:

class BaseAdapter<T extends SugarRecord & ModelInterface> extends RecyclerView.Adapter<BaseAdapter.ViewHolder> {

protected SparseArray<T> modelSparseArray;
protected Context context;

private int backgroundResource;



public BaseAdapter(SparseArray<T> modelSparseArray, Context context) {
this.modelSparseArray = modelSparseArray;
this.context = context;
setupBackgroundResource(context);
}

private void setupBackgroundResource(Context context){
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true);
backgroundResource = typedValue.resourceId;
}

public class ViewHolder extends RecyclerView.ViewHolder{

@Bind(R.id.done_indicator)
CheckBox doneIndicator;

@Bind(R.id.title)
TextView title;

View view;

public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
view = itemView;
}
}

public void addModel(T model){
int key = model.getId().intValue();
int index = modelSparseArray.indexOfKey(key);
modelSparseArray.append(key, model);
if(key < 0)
notifyItemInserted(modelSparseArray.size());
else
notifyItemChanged(index);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_layout, parent, false);
view.setBackgroundResource(backgroundResource);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final T model = modelSparseArray.valueAt(position);
holder.title.setText(model.getTitle());
holder.doneIndicator.setChecked(model.isDone());
holder.doneIndicator.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
model.setDone(isChecked);
model.save();
}
});
}



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

question几乎与我问的完全一样,但是,该问题的 OP 使用 pastebin 给出了问题的答案,不幸的是,该链接不再有效。

错误是:

Error:(24, 1) error: BaseAdapter is not abstract and does not override abstract method onBindViewHolder(BaseAdapter.ViewHolder,int) in Adapter
Error:(80, 5) error: method does not override or implement a method from a supertype

第一个错误指向类声明的开始,第二个错误指​​向onBindViewHolder上面的Override

最佳答案

所以我一发布问题就发现了答案,我会发布这个以防有人需要答案:

更改 RecyclerView.Adapter<BaseAdapter.ViewHolder>RecyclerView.Adapter<BaseAdapter<T>.ViewHolder>

我最初创建的基本适配器没有类型参数,所以它可以与 RecyclerView.Adapter<BaseAdapter.ViewHolder> 一起使用, 但在添加类型参数后,我还需要添加 <T> .

另一个解决方案是制作内部 ViewHolder正如@chRyNaN 建议的那样是静态的。

关于java - 如何创建具有扩展另一个类并实现接口(interface)的类型参数的基本适配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36156219/

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