Java 无法将Firebase数据库中的数据检索到我的Android应用程序中
我已将数据存储到Firebase数据库中,但无法检索一组数据。Java 无法将Firebase数据库中的数据检索到我的Android应用程序中,java,firebase,android-studio,firebase-realtime-database,Java,Firebase,Android Studio,Firebase Realtime Database,我已将数据存储到Firebase数据库中,但无法检索一组数据。并且图像没有显示。我只能看到添加到购物车的按钮和优雅按钮,但它显示了在活动_产品_类别中写入的文本,即它静态显示产品价格、产品名称、产品描述,并且不会替换为数据库中的实际值 下面我已附加Firebase结构。请帮忙 ProductsDetailsActivity.java: package com.example.giftngame; import androidx.annotation.NonNull; import andro
并且图像没有显示。我只能看到
添加到购物车的按钮和优雅按钮
,但它显示了在活动_产品_类别
中写入的文本,即它静态显示产品价格、产品名称、产品描述,并且不会替换为数据库中的实际值
下面我已附加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) {
}
});
}
}
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;
}
}
Products.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) {
}
});
}
}
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结构产品,子项是一个itemtype(Keychain),它具有类似于item1、item2的子项。您没有正确查询firebase数据库。应该是
DatabaseReference productsRef= FirebaseDatabase.getInstance().getReference().child("Products");
productsRef.child(itemType(like Keychain)).child(productID).addValueEventListener
itemType(Keychain)是固定的还是动态的?如果是动态的,则代码将不同。让我知道。您使用了两次那里的儿童产品。这是错误的
final DatabaseReference productsRef= FirebaseDatabase.getInstance().getReference().child("Products");
productsRef.child("Products").child(productID).addValueEventListener(new ValueEventListener() {...
我知道您想显示产品的详细信息。您可以对您的条件使用查询。试试这个:-
private void getProductDetails(final String productID)
{
final DatabaseReference productsRef= FirebaseDatabase.getInstance().getReference().child("Products");
final Query queryProductDetail = productRef.orderByChild("pid").equalTo(productID);
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) {
}
});
}
钥匙链项目是众多项目中的一项,共有8项(钥匙链、马克杯、镇纸、笔架、香水、相框、泰迪熊、风铃)这些都是产品的孩子ProductDetailsActivity.java被设置为对所有人来说都是通用的。所以我没有选择特别写itemType Keychain你是如何开始ProductDetails活动的我不明白你的问题你是如何开始ProductDetailsActivity活动的。这是你的主要活动吗?