Java 适配器没有';不显示Firebase实时数据库中的产品图像和价格
我试图从Firebase实时数据库中检索数据(产品图像及其价格),并将其放入recyclerView中,但我遇到了一些困难。在我的数据库中,我存储了一个产品的图像url,我想在cardView中的ImageView组件中显示该图像。我还设法正确地检索了url和价格,但我的recyclerView没有显示它们。 这是我的产品类别:Java 适配器没有';不显示Firebase实时数据库中的产品图像和价格,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我试图从Firebase实时数据库中检索数据(产品图像及其价格),并将其放入recyclerView中,但我遇到了一些困难。在我的数据库中,我存储了一个产品的图像url,我想在cardView中的ImageView组件中显示该图像。我还设法正确地检索了url和价格,但我的recyclerView没有显示它们。 这是我的产品类别: public class CustomizeProduct { private String productImage; private String
public class CustomizeProduct {
private String productImage;
private String productName;
private String productPrice;
public CustomizeProduct(String image, String name, String price){
this.productImage = image;
this.productName = name;
this.productPrice = price;
}
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 getProductPrice() { return productPrice; }
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
}
我的适配器如下所示:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private ArrayList<CustomizeProduct> customized;
private Context myContext;
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView thisProductImage;
public TextView thisProductName;
public TextView thisProductPrice;
public ViewHolder(View itemView) {
super(itemView);
thisProductImage = itemView.findViewById(R.id.customProductImage);
thisProductName = itemView.findViewById(R.id.customProductName);
thisProductPrice = itemView.findViewById(R.id.customProductPrice);
}
}
public ProductAdapter(Context context, ArrayList<CustomizeProduct> thisCustom) {
myContext = context;
customized = thisCustom;
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredients, parent, false);
ViewHolder mv = new ViewHolder(v);
return mv;
}
public void onBindViewHolder(ViewHolder holder, int position) {
CustomizeProduct currentCustom = customized.get(position);
Picasso.get().load(currentCustom.getProductImage()).placeholder(R.drawable.shrimp_ditali).into(holder.thisProductImage);
holder.thisProductName.setText(currentCustom.getProductName());
holder.thisProductPrice.setText(currentCustom.getProductPrice());
}
public int getItemCount() {
return customized.size();
}
}
在onBindViewHolder中:
public void onBindViewHolder(ViewHolder holder, int position) {
CustomizeProduct currentCustom = customized.get(position);
Picasso.get().load(currentCustom.getIMAGINE()).placeholder(R.drawable.shrimp_ditali).into(holder.thisProductImage);
holder.thisProductName.setText(currentCustom.getNUME_PRODUS());
holder.thisProductPrice.setText(currentCustom.getPRET_PRODUS());
}
我认为您可能还需要注释私有字段,因为SDK使用这些字段来设置数据库中的值:
public class CustomizeProduct {
@PropertyName("IMAGINE")
private String imagine;
@PropertyName("NUME_PRODUS")
private String nume_produs;
@PropertyName("PRET_PRODUS")
private String pret_produs;
public CustomizeProduct(){}
public CustomizeProduct(String imagine, String nume_produs, String pret_produs){
this.imagine = imagine;
this.nume_produs = nume_produs;
this.pret_produs = pret_produs;
}
@PropertyName("IMAGINE")
public String getIMAGINE() {
return imagine;
}
@PropertyName("NUME_PRODUS")
public String getNUME_PRODUS() {
return nume_produs;
}
@PropertyName("PRET_PRODUS")
public String getPRET_PRODUS() { return pret_produs; }
}
请注意,使用getter和setter是完全可选的,您还可以使用这个(更短)类绑定到相同的JSON结构:
public class CustomizeProduct {
@PropertyName("IMAGINE")
public String imagine;
@PropertyName("NUME_PRODUS")
public String nume_produs;
@PropertyName("PRET_PRODUS")
public String pret_produs;
}
我认为您可能还需要注释私有字段,因为SDK使用这些字段来设置数据库中的值:
public class CustomizeProduct {
@PropertyName("IMAGINE")
private String imagine;
@PropertyName("NUME_PRODUS")
private String nume_produs;
@PropertyName("PRET_PRODUS")
private String pret_produs;
public CustomizeProduct(){}
public CustomizeProduct(String imagine, String nume_produs, String pret_produs){
this.imagine = imagine;
this.nume_produs = nume_produs;
this.pret_produs = pret_produs;
}
@PropertyName("IMAGINE")
public String getIMAGINE() {
return imagine;
}
@PropertyName("NUME_PRODUS")
public String getNUME_PRODUS() {
return nume_produs;
}
@PropertyName("PRET_PRODUS")
public String getPRET_PRODUS() { return pret_produs; }
}
请注意,使用getter和setter是完全可选的,您还可以使用这个(更短)类绑定到相同的JSON结构:
public class CustomizeProduct {
@PropertyName("IMAGINE")
public String imagine;
@PropertyName("NUME_PRODUS")
public String nume_produs;
@PropertyName("PRET_PRODUS")
public String pret_produs;
}
customized.add(新的CustomizeProduct(productImage,checkout.thisProductName,productPrice))代码>
您只能看到产品名称(checkout.thisProductName
),而不能看到产品图像和价格,因为您使用Firebase在onDataChange(DataSnapshot ds)
回调中未返回的产品名称填充适配器;代码中没有关于如何生成产品名称(checkout.thisProductName
)值的线索
现在,在主线程中构建适配器时,产品图像和价格不会显示,而firebase在后台线程中隐式工作;因此,您需要将构建recyclerView适配器的部分代码传输到firebase回调中,从而将适配器同步到该后台线程,如下所示:
public void buildCustom(){
recyclerView = findViewById(R.id.customList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("MENIU");
ref.addListenerForSingleValueEvent(new ValueEventListener(){
public void onDataChange(DataSnapshot ds) {
for (DataSnapshot menu : ds.getChildren()) {
String keys = menu.getKey();
String productName = (String) ds.child(keys).child("NUME_PRODUS").getValue();
if(productName.equals(checkout.thisProductName)){
productImage = ds.child(keys).child("IMAGINE").getValue(String.class);
Long price = (Long) ds.child(keys).child("PRET_PRODUS").getValue();
productPrice = String.valueOf(price);
Toast.makeText(getApplicationContext(), productImage + " " + productPrice, Toast.LENGTH_LONG).show();
}
}
// here you've received all Firebase data and can now build the recyclerView
for(int i = 0; i < checkout.thisProductQty; i++) {
customized.add(new CustomizeProduct(productImage,checkout.thisProductName, productPrice));
myAdapter = new ProductAdapter(getApplicationContext(), customized);
}
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(myAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public void buildCustom(){
recyclerView=findViewById(R.id.customList);
recyclerView.setHasFixedSize(true);
layoutManager=新的LinearLayoutManager(此);
final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child(“MENIU”);
ref.addListenerForSingleValueEvent(新的ValueEventListener(){
公共无效onDataChange(DataSnapshot ds){
对于(DataSnapshot菜单:ds.getChildren()){
字符串键=menu.getKey();
String productName=(字符串)ds.child(键).child(“NUME\U PRODUS”).getValue();
if(productName.equals(checkout.thisProductName)){
productImage=ds.child(keys.child(“想象”).getValue(String.class);
Long price=(Long)ds.child(key.child(“PRET_PRODUS”).getValue();
productPrice=String.valueOf(price);
Toast.makeText(getApplicationContext(),productImage+“”+productPrice,Toast.LENGTH_LONG).show();
}
}
//在这里,您已收到所有Firebase数据,现在可以构建recyclerView
对于(int i=0;i
customized.add(新的CustomizeProduct(productImage,checkout.thisProductName,productPrice))代码>
您只能看到产品名称(checkout.thisProductName
),而不能看到产品图像和价格,因为您使用Firebase在onDataChange(DataSnapshot ds)
回调中未返回的产品名称填充适配器;代码中没有关于如何生成产品名称(checkout.thisProductName
)值的线索
现在,在主线程中构建适配器时,产品图像和价格不会显示,而firebase在后台线程中隐式工作;因此,您需要将构建recyclerView适配器的部分代码传输到firebase回调中,从而将适配器同步到该后台线程,如下所示:
public void buildCustom(){
recyclerView = findViewById(R.id.customList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("MENIU");
ref.addListenerForSingleValueEvent(new ValueEventListener(){
public void onDataChange(DataSnapshot ds) {
for (DataSnapshot menu : ds.getChildren()) {
String keys = menu.getKey();
String productName = (String) ds.child(keys).child("NUME_PRODUS").getValue();
if(productName.equals(checkout.thisProductName)){
productImage = ds.child(keys).child("IMAGINE").getValue(String.class);
Long price = (Long) ds.child(keys).child("PRET_PRODUS").getValue();
productPrice = String.valueOf(price);
Toast.makeText(getApplicationContext(), productImage + " " + productPrice, Toast.LENGTH_LONG).show();
}
}
// here you've received all Firebase data and can now build the recyclerView
for(int i = 0; i < checkout.thisProductQty; i++) {
customized.add(new CustomizeProduct(productImage,checkout.thisProductName, productPrice));
myAdapter = new ProductAdapter(getApplicationContext(), customized);
}
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(myAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public void buildCustom(){
recyclerView=findViewById(R.id.customList);
recyclerView.setHasFixedSize(true);
layoutManager=新的LinearLayoutManager(此);
final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child(“MENIU”);
ref.addListenerForSingleValueEvent(新的ValueEventListener(){
公共无效onDataChange(DataSnapshot ds){
对于(DataSnapshot菜单:ds.getChildren()){
字符串键=menu.getKey();
String productName=(字符串)ds.child(键).child(“NUME\U PRODUS”).getValue();
if(productName.equals(checkout.thisProductName)){
productImage=ds.child(keys.child(“想象”).getValue(String.class);
Long price=(Long)ds.child(key.child(“PRET_PRODUS”).getValue();
productPrice=String.valueOf(price);
Toast.makeText(getApplicationContext(),productImage+“”+productPrice,Toast.LENGTH_LONG).show();
}
}
//在这里,您已收到所有Firebase数据,现在可以构建recyclerView
对于(int i=0;i
Y