我已将数据存储到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) {
}
});
}
}
Products.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;
}
}
这是该商品的详细说明之一
答案 0 :(得分:0)
根据您的Firebase结构,Products子项是一个itemtype(钥匙串),其子项类似于item1,item2。您没有正确查询Firebase数据库。应该是
DatabaseReference productsRef= FirebaseDatabase.getInstance().getReference().child("Products");
productsRef.child(itemType(like Keychain)).child(productID).addValueEventListener
itemType(Keychain)是固定的还是动态的?如果它是动态的,则代码将不同。让我知道。
答案 1 :(得分:0)
您在那里使用了孩子Products
的两倍。是错的。
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) {
}
});
}