无法解析onBindViewHolder中的符号“ textView”

时间:2018-12-06 06:48:43

标签: java android android-recyclerview recycler-adapter

我仍在学习创建移动应用程序,所以我想我在这里犯了一些小错误...

我正在尝试制作一个recyclerView,但是适配器中有一个错误。 我一直在这里寻找答案,对我没有任何帮助。这是我的代码

package com.abelherl.carttest;

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;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class ItemAdapter extends RecyclerView.Adapter {
private ArrayList<Item> items;

public ItemAdapter(ArrayList<Item> items){
    this.items = items;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    public final View view;
    public final TextView textView8;
    public final TextView textView7;
    public final TextView textView10;

    public ViewHolder(@NonNull View view) {
        super(view);
        this.view = view;
        textView8 = view.findViewById(R.id.textView8);
        textView7 = view.findViewById(R.id.textView7);
        textView10 = view.findViewById(R.id.textView10);
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    View v = (View) LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    Item item = items.get(position);

    holder.textView8.setText(item.getTextView8());
    holder.textView7.setText(item.getTextView7());
    holder.textView10.setText(item.getTextView10());
}

@Override
public int getItemCount() {
    if(items != null){
        return items.size();
    } else {
        return 0;
    }
}


}

感谢您的回答。

5 个答案:

答案 0 :(得分:0)

请使项目适配器像

public class ItemAdapter extends RecyclerView.Adapter<Itemadpater.ViewHolder> { your code}

答案 1 :(得分:0)

将RecyclerView.Adapter更改为RecyclerView.Adapter<ItemAdapter.ViewHolder>

答案 2 :(得分:0)

您需要添加适配器名称和持有者名称。我进行了更改,它将起作用。

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private ArrayList<Item> items;

public ItemAdapter(ArrayList<Item> items){
    this.items = items;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    public final View view;
    public final TextView textView8;
    public final TextView textView7;
    public final TextView textView10;

    public ViewHolder(@NonNull View view) {
        super(view);
        this.view = view;
        textView8 = view.findViewById(R.id.textView8);
        textView7 = view.findViewById(R.id.textView7);
        textView10 = view.findViewById(R.id.textView10);
    }
}

@NonNull
@Override
public ItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    View v = (View) LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Item item = items.get(position);

    holder.textView8.setText(item.getTextView8());
    holder.textView7.setText(item.getTextView7());
    holder.textView10.setText(item.getTextView10());
}

@Override
public int getItemCount() {
    if(items != null){
        return items.size();
    } else {
        return 0;
    }
}

}

答案 3 :(得分:0)

将您重写的函数提供的ViewHolder转换为ViewHolder类型,然后使用它。

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    Item item = items.get(position);

    /* Cast first */
    ViewHolder viewHolder = (ViewHolder) holder;

    /* Then use */
    viewHolder.textView8.setText(item.getTextView8());
    viewHolder.textView7.setText(item.getTextView7());
    viewHolder.textView10.setText(item.getTextView10());
}

您的ViewHolder类型为ViewHolder(您可以将类名更改为更合适的名称),而函数替代会提供您扩展的类型,即RecyclerView.ViewHolder

答案 4 :(得分:0)

我认为您应该像这样扩展ItemAdapter:

public class ItemAdapter extends RecyclerView.Adapter<Itemadpater.ViewHolder>