如何创建具有扩展另一个类并实现接口的类型参数的基础适配器?

时间:2016-03-22 13:50:34

标签: java android generics

我已经看到问题的answer

  

Java:如何创建一个需要扩展类并实现接口的数据类型的泛型类?

所以我尝试为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

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

2 个答案:

答案 0 :(得分:6)

所以我在发布问题后立即发现答案,我会发布这个以防万一有人需要答案:

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

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

另一种解决方案是将内部ViewHolder静态化为@chRyNaN建议。

答案 1 :(得分:-3)

您是否尝试从

更改方法签名?
public void onBindViewHolder(ViewHolder holder, final int position)

public void onBindViewHolder(ViewHolder holder, int position)

只需输入第二个参数NOT final