当前位置: 首页 > 知识库问答 >
问题:

Gradle构建错误失败,因为“@override方法没有重写超类中的方法”

赵俊晤
2023-03-14

代码如下。IDE对代码很好,但gradle拒绝构建,他说:

TextAdapter不是抽象的,并且不重写适配器公共类中的抽象方法onBindViewHolder(TextAdapter.SimpleTextholder,int)TextAdapter extends RecycerView.Adapter{

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import ...R;
import ...jsonObjects.Nameable;

import java.util.ArrayList;

public class TextAdapter<T extends Nameable> extends RecyclerView.Adapter<TextAdapter.SimpleTextHolder> {

    private ArrayList<T> items;
    private ClickListener<T> clickListener;

    public TextAdapter(ArrayList<T> items, ClickListener<T> onClick) {
        this.items = items;
        this.clickListener = onClick;
    }

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

    @Override
    public void onBindViewHolder(SimpleTextHolder holder, int position) {
        holder.itemTitle.setText(items.get(position).getName());
    }


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

    class SimpleTextHolder extends RecyclerView.ViewHolder {

        TextView itemTitle;

        SimpleTextHolder(View itemView) {
            super(itemView);

            itemTitle = itemView.findViewById(R.id.itemTitle);
            itemView.setOnClickListener(v -> clickListener.onClick(items.get(getAdapterPosition())));
        }

    }

    public interface ClickListener<T> {

        void onClick(T item);

    }

}

共有1个答案

吴欣然
2023-03-14

您可以通过使SimpleTexTholder为静态来解决这个问题,即。

static class SimpleTextHolder extends RecyclerView.ViewHolder {
    // ...
}
 类似资料: