检索儿童信息firebase android

时间:2017-11-08 12:49:09

标签: android firebase firebase-realtime-database

我有这段代码,我想阅读 Firebase 中的所有产品,并在 ListView 中显示他们的信息,但我无法和#39; t达到产品信息,我该怎么办? 这是我的代码:

public class Product_List extends AppCompatActivity {

    DatabaseReference mDatabase;
    ListView productsList;
    ArrayList<Product> products;
    DatabaseReference productsRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product__list);
//        ListView listView = (ListView) findViewById(R.id.listView);
        mDatabase = FirebaseDatabase.getInstance().getReference();
        productsRef = mDatabase.child("products");
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    String productID = ds.getKey();
                    DatabaseReference Product = productsRef.child(productID);
                    String productName = Product.child("product_name").toString();
                    Product a = new Product(productName,"a","a","a",1,1);
                    products.add(a);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        };
        productsRef.addListenerForSingleValueEvent(eventListener);

        LinearLayout productList = (LinearLayout) findViewById(R.id.ProductList);
        productAdapter itemsAdapter = new productAdapter(this, products);
        productsList = (ListView) findViewById(listView);
        productsList.setAdapter(itemsAdapter);
//

    }
}

但是当我这样做时我的应用程序崩溃了!

here a screenshot of my database

更新:

当我尝试这样做时:

    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {

            Product product = ds.getValue(Product.class);
            products.add(product);
        }

我收到了这个错误:(

11-08 16:21:10.374 5270-5270/com.example.kamran.bluewhite E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.kamran.bluewhite, PID: 5270
                                                                            com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to int
                                                                                at com.google.android.gms.internal.zzbqi.zzaz(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi.zzc(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                                at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                                at com.example.kamran.bluewhite.Product_List$1.onDataChange(Product_List.java:57)
                                                                                at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
                                                                                at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                                                                                at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
11-08 16:21:10.376 1654-2210/system_process W/ActivityManager:   Force finishing activity com.example.kamran.bluewhite/.Product_List

product_List的第57行是:

 Product product = ds.getValue(Product.class);

3 个答案:

答案 0 :(得分:1)

要获取产品内的数据,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference productsRef = rootRef.child("products");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            int ammount = ds.child("ammount").getValue(Integer.class);
            String brand = ds.child("brand").getValue(String.class);
            String category = ds.child("category").getValue(String.class);
            String description = ds.child("description").getValue(String.class);
            int price = ds.child("price").getValue(Integer.class);
            String product_name = ds.child("product_name").getValue(String.class);
            Log.d("TAG", ammount + " / " + 
                     brand + " / " + 
                     category + " / " + 
                     description + " / " + 
                     price + " / " + 
                     product_name);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
productsRef.addListenerForSingleValueEvent(eventListener);

您的输出将是:

 1 / fdgh / ghgh / rawan / 11 / r
//and so on for other products

答案 1 :(得分:1)

for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String productID = ds.getKey();
                DatabaseReference Product = productsRef.child(productID);
                String productName = Product.child("product_name").toString();
                Product a = new Product(productName,"a","a","a",1,1);
                products.add(a);
            }

我不会那样实现。首先,我将创建一个将成为产品模型的Java类。

class Product{
private double amount;
private String brand;
private String category;
private String description;
private double price;
private String product_name;

public Product(){
}
//Setters and getters for each field
}

创建Product类之后,这是检索数据的代码。

        ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                Product product = ds.getValue(Product.class);
                //Here you have successfully retrieve one Product and from here you can do 
                //anything you need.
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };

在此链接https://firebase.google.com/docs/database/admin/retrieve-data中,您可以查看Firebase中有关如何检索数据的说明。

答案 2 :(得分:0)

为什么不试试ChildEventListener

ChildEventListener childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());

        // A new comment has been added, add it to the displayed list
        Product roduct = dataSnapshot.getValue(Product.class);

        // ...
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
        Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "postComments:onCancelled", databaseError.toException());
    }
};
mDatabase = FirebaseDatabase.getInstance().getReference();
productsRef = mDatabase.child("products");
productsRef.addChildEventListener(childEventListener);

请参阅https://firebase.google.com/docs/database/android/lists-of-data