gpt4 book ai didi

java - 无法将 Firebase 数据库中的数据检索到我的 Android 应用程序中

转载 作者:行者123 更新时间:2023-12-01 19:06:03 27 4
gpt4 key购买 nike

我已将数据存储到 Firebase 数据库中,但无法检索一组数据。
getPname()getDescription()getPrice() 并且图像未显示。我只能看到 Add to cart 按钮和 ElegantButton 但它显示了文本编写在activity_products_category中,即静态显示产品价格、产品名称、产品描述,并且不会将其替换为数据库中的实际值。

下面我附上了 Firebase 结构。请帮忙。

ProductsDetailsActivity.java:

 package com.example.giftngame;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.example.giftngame.Model.Products;
import com.example.giftngame.Prevalent.Prevalent;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

public class ProductDetailsActivity extends AppCompatActivity
{
private Button addToCartButton;
private ImageView productImage;
private ElegantNumberButton numberButton;
private TextView productPrice,productDescription,productName;
private String productID="";

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

productID = getIntent().getStringExtra("pid");

addToCartButton = (Button) findViewById(R.id.pd_add_to_cart_button);
numberButton = (ElegantNumberButton) findViewById(R.id.number_btn);
productImage = (ImageView) findViewById(R.id.product_image_details);
productPrice = (TextView) findViewById(R.id.product_price);
productDescription = (TextView) findViewById(R.id.product_description_details);
productName = (TextView) findViewById(R.id.product_name_details);

getProductDetails(productID);
}

private void getProductDetails(String productID)
{
final DatabaseReference productsRef= FirebaseDatabase.getInstance().getReference().child("Products");
productsRef.child("Products").child(productID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange( DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
Products products=dataSnapshot.getValue(Products.class);

productName.setText(products.getPname());
productDescription.setText(products.getDescription());
productPrice.setText(products.getPrice());
Picasso.get().load(products.getImage()).into(productImage);

}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}

}

产品.java:

package com.example.giftngame.Model;

public class Products
{
private String pid,pname,price,description,image,category,date,time;


public Products()
{

}

public Products(String pname, String description, String price, String image, String category, String pid, String date, String time)
{
this.pname = pname;
this.description = description;
this.price = price;
this.image = image;
this.category = category;
this.pid = pid;
this.date = date;
this.time = time;
}

public String getPname() {
return pname;
}

public void setPname(String pname) {
this.pname = pname;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getPid() {
return pid;
}

public void setPid(String pid) {
this.pid = pid;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}
}

这是该项目的详细描述之一

This is the one of the Item's description in detail

最佳答案

根据您的 firebase 结构,Products 子项是一个 itemtype(Keychain),其中包含 item1、item2 等子项。您没有正确查询 firebase 数据库。应该是这样的

 DatabaseReference productsRef= FirebaseDatabase.getInstance().getReference().child("Products");
productsRef.child(itemType(like Keychain)).child(productID).addValueEventListener

itemType(Keychain) 是固定的还是动态的?如果是动态的,那么代码将会不同。让我知道。

关于java - 无法将 Firebase 数据库中的数据检索到我的 Android 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59554143/

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