英文:
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<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);
}
}
}
答案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
<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" />
答案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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论