android 无法解析onBindViewHolder中的符号'textView'

xmjla07d  于 2022-11-20  发布在  Android
关注(0)|答案(5)|浏览(225)

我还在学习创建一个移动的应用程序,所以我想我在这里犯了一些小错误...
我试图使一个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;
    }
}

}

谢谢你的回答。

3npbholx

3npbholx1#

ItemAdapter设为如下所示:

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> { … }
myss37ts

myss37ts2#

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

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

6ie5vjzr3#

将您覆盖的函数提供的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

cqoc49vn

cqoc49vn4#

将回收机视图适配器更改为RecyclerView.Adapter<ItemAdapter.ViewHolder>

wrrgggsh

wrrgggsh5#

您需要添加适配器名称与保持器的名称。我已经作出了改变,它将工作。

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

}

相关问题