RecycleView中的onBindViewHolder问题

时间:2019-04-01 19:57:31

标签: java android android-recyclerview inner-classes

我无法将textView放在内部类Item的onBindViewHolder方法中,将其公开并最终确定,仍然无法解决,我尝试了许多示例,这是相同的问题。

package com.example.recyclerview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

    Context context;
    String [] items;
    public Adapter(Context context, String[] items){
        this.context = context;
        this.items = items;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View row = inflater.inflate(R.layout.custom_row, viewGroup, false);
        Item item = new Item(row);
        return item;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        String mCurrent = items[i];
        viewHolder.textView.setText(mCurrent); // error here textView cannot resolve
    }

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

    public class Item extends RecyclerView.ViewHolder {
        public final TextView textView;
        public Item(@NonNull View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.item);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您应该更正适配器的通用类型:使用Adapter.Item代替RecyclerView.ViewHolder

public class Adapter extends RecyclerView.Adapter<Adapter.Item>

然后更新onCreateViewHolderonBindViewHolder方法的签名