What is this "RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView" error?

huangapple go评论74阅读模式
英文:

What is this "RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView" error?

问题

private List<NewsModel> newsModels;

public NewsAdapter(List<NewsModel> newsModels) {
    this.newsModels = newsModels;
}

@NonNull
@Override
public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news, parent, false);
    return new NewsViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
    NewsModel news = newsModels.get(position);

    holder.textViewName.setText(news.getName());
    holder.textViewBrand.setText(news.getBrand());
    holder.textViewDesc.setText(news.getDescription());
    holder.textViewPrice.setText("INR " + news.getPrice());
    holder.textViewQty.setText("Available Units: " + news.getQty());
}

@Override
public int getItemCount() {
    return newsModels.size();
}

class NewsViewHolder extends RecyclerView.ViewHolder {

    TextView textViewName, textViewBrand, textViewDesc, textViewPrice, textViewQty;

    public NewsViewHolder(View itemView) {
        super(itemView);

        textViewName = itemView.findViewById(R.id.textview_name);
        textViewBrand = itemView.findViewById(R.id.textview_brand);
        textViewDesc = itemView.findViewById(R.id.textview_desc);
        textViewPrice = itemView.findViewById(R.id.textview_price);
        textViewQty = itemView.findViewById(R.id.textview_quantity);

    }
}
}
英文:

I'm trying to retrieve data from firebase. But it shows "RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView" error? Can somebody help me to fix this error please?
Error Image

NewsAdapter.java

private List&lt;NewsModel&gt; newsModels;
public NewsAdapter( List&lt;NewsModel&gt; newsModels) {
this.newsModels = newsModels;
}
@NonNull
@Override
public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news,parent,false);
return new NewsViewHolder (view);
}
@Override
public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
NewsModel news = newsModels.get(position);
holder.textViewName.setText(news.getName());
holder.textViewBrand.setText(news.getBrand());
holder.textViewDesc.setText(news.getDescription());
holder.textViewPrice.setText(&quot;INR &quot; + news.getPrice());
holder.textViewQty.setText(&quot;Available Units: &quot; + news.getQty());
}
@Override
public int getItemCount() {
return newsModels.size();
}
class NewsViewHolder extends RecyclerView.ViewHolder {
TextView textViewName, textViewBrand, textViewDesc, textViewPrice, textViewQty;
public NewsViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.textview_name);
textViewBrand = itemView.findViewById(R.id.textview_brand);
textViewDesc = itemView.findViewById(R.id.textview_desc);
textViewPrice = itemView.findViewById(R.id.textview_price);
textViewQty = itemView.findViewById(R.id.textview_quantity);
}
}
}

答案1

得分: 0

如果您尚未添加 RecyclerView,则可以通过以下 XML 代码添加它:

<androidx.recyclerview.widget.RecyclerView
 android:id="@+id/contentContainer"
 android:orientation="vertical"
 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
英文:

if you haven't added a recycler view then you could just add it through xml code like this

&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/contentContainer&quot;
android:orientation=&quot;vertical&quot;
app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;

答案2

得分: 0

在您希望使用 RecyclerView 的活动中:

RecyclerView recyclerView = findViewById(R.id.recyclerVeiw);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new YourAdapeter());
英文:

in your activity that you want use recyclerView:

RecyclerView recyclerView  =   findViewById(R.id.recyclerVeiw);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new YourAdapeter());

huangapple
  • 本文由 发表于 2020年9月27日 22:55:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64089818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定