gpt4 book ai didi

java - 来自 Firebase 的数据未显示在 RecyclerView 中

转载 作者:行者123 更新时间:2023-12-02 01:19:05 25 4
gpt4 key购买 nike

我的 RecyclerView 未显示来自 Firebase 的任何数据。我在 logcat 上没有看到任何错误,所以我不知道为什么。我遵循 youtube 的指南视频。但还是什么都没有。我正在使用 android 8.0 和 API 27。希望有人可以提供帮助。

我的主要 Activity 代码:

public class ViewProduct extends AppCompatActivity {

DatabaseReference ref;
ArrayList<Model> list;
private RecyclerView recyclerView;
private RecyclerView.Adapter viewHolder;
private RecyclerView.LayoutManager layoutManager;
SearchView searchView;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_product) ;

ref = FirebaseDatabase.getInstance().getReference().child("buddymealplanneruser").
child("Products");


recyclerView = findViewById(R.id.stallproductRecyclerView);
//recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

//SEARCH VIEW
searchView = findViewById(R.id.searchProductStall);

//ADAPTER
list = new ArrayList<>();
viewHolder = new ViewHolder(list);
recyclerView.setAdapter(viewHolder);



}

@Override
protected void onRestart() {
super.onRestart();
if (ref!=null)
{
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
//list = new ArrayList<>();
list.clear();
for(DataSnapshot ds : dataSnapshot.getChildren())
{

list.add(ds.getValue(Model.class));

}
//ViewHolder viewHolder = new ViewHolder(list);
//recyclerView.setAdapter(viewHolder);

viewHolder.notifyDataSetChanged();

}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ViewProduct.this, databaseError.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
if (searchView !=null)
{
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s)
{
return false;
}

@Override
public boolean onQueryTextChange(String s) {
search(s);
return true;
}
});
}
}

private void search(String str) {
ArrayList<Model> myList = new ArrayList<>();
for(Model object : list)
{
if(object.getProductName().toLowerCase().contains(str.toLowerCase()))
{
myList.add(object);
}
}
ViewHolder viewHolder = new ViewHolder(myList);
recyclerView.setAdapter(viewHolder);


}


}

我的适配器:

public class ViewHolder extends  RecyclerView.Adapter<ViewHolder.MyViewHolder> {


ArrayList<Model> list;
public ViewHolder (ArrayList<Model> list)
{
this.list=list;
}




@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_product, viewGroup,false);

return new MyViewHolder(view);


}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {

myViewHolder.productName.setText(list.get(i).getProductName());
myViewHolder.productCalorie.setText(list.get(i).getProductCalorie());
myViewHolder.StallId.setText(list.get(i).getStallId());
myViewHolder.productType.setText(list.get(i).getProductType());
myViewHolder.productServing.setText(list.get(i).getProductServing());
myViewHolder.statusProduct.setText(list.get(i).getStatusProduct());


}

@Override
public int getItemCount()
{

return list.size();
}

class MyViewHolder extends RecyclerView.ViewHolder
{
TextView productName, productCalorie, StallId;
TextView productType, productServing, statusProduct;
ImageView productImageView;

public MyViewHolder (@NonNull View itemView){
super((itemView));

productName = itemView.findViewById(R.id.productTitle);
productCalorie = itemView.findViewById(R.id.productDescriptionCalorie);
StallId = itemView.findViewById(R.id.productDescriptionStallId);
productType = itemView.findViewById(R.id.productDescriptionType);
productServing=itemView.findViewById(R.id.productDescriptionServing);
statusProduct=itemView.findViewById(R.id.productDescriptionAvailibility);
//productImageView = itemView.findViewById(R.id.productImageView);
}


}
}

我的模型:

package com.example.buddymealplannerstall.Child;

import android.view.Display;

public class Model {

//String productName, productImage, productCalorie, StallId, productType, productServing, statusProduct;
private String StallId;
private String productCalorie;
private String productImage;
private String productName;
private String productServing;
private String productType;
private String statusProduct;

public Model (String StallId,
String productCalorie,
String productImage,
String productName,
String productServing,
String productType,
String statusProduct){

this.StallId = StallId;
this.productCalorie = productCalorie;
this.productImage = productImage;
this.productName = productName;
this.productServing = productServing;
this.productType = productType;
this.statusProduct = statusProduct;

}

public Model(){

}

public String getStallId() {
return StallId;
}

public void setStallId(String stallId) {
StallId = stallId;
}

public String getProductCalorie() {
return productCalorie;
}

public void setProductCalorie(String productCalorie) {
this.productCalorie = productCalorie;
}

public String getProductImage() {
return productImage;
}

public void setProductImage(String productImage) {
this.productImage = productImage;
}

public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public String getProductServing() {
return productServing;
}

public void setProductServing(String productServing) {
this.productServing = productServing;
}

public String getProductType() {
return productType;
}

public void setProductType(String productType) {
this.productType = productType;
}

public String getStatusProduct() {
return statusProduct;
}

public void setStatusProduct(String statusProduct) {
this.statusProduct = statusProduct;
}
}

我的 Firebase 和应用程序的屏幕截图。

firebase my app

最佳答案

根据您的评论:

yes, buddymealplanner is my project's name

请注意,无需在引用中添加项目名称作为子项目。所以请更改以下代码行:

ref = FirebaseDatabase.getInstance().getReference().child("buddymealplanneruser").
child("Products");

简单地:

ref = FirebaseDatabase.getInstance().getReference().child("Products");

关于java - 来自 Firebase 的数据未显示在 RecyclerView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58046067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com