代码如下。 IDE可以很好地使用代码,但gradle拒绝构建说法:
TextAdapter不是抽象的,并且不会在适配器中覆盖抽象方法onBindViewHolder(TextAdapter.SimpleTextHolder,int) 公共类TextAdapter扩展了RecyclerView.Adapter {
错误:方法不会覆盖或实现超类型
中的方法
我知道这是错的,因为我使用Android Studio生成了这个方法所以我认为它必须是一个gradle错误?
RecyclerView适配器代码:
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);
}
}
答案 0 :(得分:1)
您可以通过制作SimpleTextHolder
班static
,即
static class SimpleTextHolder extends RecyclerView.ViewHolder {
// ...
}