java - 无法将 Firebase 数据库中的数据检索到我的 Android 应用程序中
问题描述
我已将数据存储到 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;
}
}
这是项目的详细描述之一
解决方案
根据您的 Firebase 结构 Products 子项是一个 itemtype(Keychain) ,其中包含 item1、item2 等子项。您没有正确查询 firebase 数据库。它应该像
DatabaseReference productsRef= FirebaseDatabase.getInstance().getReference().child("Products");
productsRef.child(itemType(like Keychain)).child(productID).addValueEventListener
itemType(Keychain) 是固定的还是动态的?如果它是动态的,那么代码将会不同。让我知道。
推荐阅读
- javascript - 赛普拉斯 - 如何不等待页面加载事件?
- r - 2个循环中的指数/指数之和 - R
- node.js - Mongo(Node Js,Keystone)字段选择和切片不能与不同的错误一起使用:
- powershell - 将函数及其参数作为函数的参数传递
- c++ - 在 pthread 中使用共享变量
- database - 无法使用 odbc 连接创建数据库
- node.js - 如何使用 knex 模块从 Node.JS 通过 Windows 身份验证连接到 SQL Server
- windows - 在 Apache Windows 10 中添加虚拟主机
- python - 带有python的openv程序
- python - CherryPy 身份验证超时