简体   繁体   中英

Problem with onBindViewHolder in RecycleView

I can't put my textView in the method onBindViewHolder from the inner class Item, I made it public and final and is still cannot resolve it, I tried many examples and it's the same problem.

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);
        }
    }

}

You should correct generic type of the adapter: use Adapter.Item instead of RecyclerView.ViewHolder

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

and then update signature of onCreateViewHolder and onBindViewHolder methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM