在firebase中获取数据并在Textview中显示有问题......,

时间:2016-06-10 15:38:36

标签: android firebase firebase-realtime-database

我已经在这个代码上工作了好几天,首先我认为它的数据库引用有问题,GetRef正在工作,所以我有点困惑,因为datasnap返回null ....,它实际上在我工作之前更新了我的数据库参考...,

 Intent i = getIntent();
            final String key = i.getExtras().getString("firekey");
            Log.d("inter", key);

            // Initialize Database
            Query queryRef = FirebaseDatabase.getInstance().getReference()
                    .child("REPORTS").orderByKey()
                    .equalTo(key);

            String dd=queryRef.toString();
            Log.d("query", dd);

    queryRef.addChildEventListener(new ChildEventListener() {

                @Override

                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Log.d(TAG, "onChildAdded:" + dataSnapshot.getRef());

                    Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
                    if (newPost != null) {
                        reportSingle = new FoodItem(newPost.get("MEAL_TITLE").toString().trim(),
                                newPost.get("DESCRIPTION").toString().trim(),
                                newPost.get("MEAL_INGREDIENTS").toString().trim(),

                                newPost.get("MEAL_PRICE").toString().trim(),
                                newPost.get("MEAL_CATEGORY").toString().trim(),
                                newPost.get("MEAL_QUANTITY").toString().trim(),
                                newPost.get("MEAL_NUTRITIONAL_VALUE").toString().trim(),
                                newPost.get("MEAL_TYPE").toString().trim(),
                                newPost.get("LOCATION").toString().trim(),
                                newPost.get("PICTURE").toString().trim(),
                                dataSnapshot.getRef(),
                                newPost.get("VOTES").toString().trim());


                        String s1 = reportSingle.getIngredients().trim();
                        Log.d("single", s1);
                        singleIngredients.setText(reportSingle.getIngredients());
                        singleQuantity.setText(reportSingle.getQuantity());
                        singleNutritionalValue.setText(reportSingle.getQuantity());
                        singleMealType.setText(reportSingle.getMeal_type());

                        singleLocation.setText(reportSingle.getLocation());
                        //singleInstitution.setText(reportSingle.getInstitution());

                        singleTitle.setText(reportSingle.getTitle());
                        singleDescription.setText(reportSingle.getDescription());


                        String photoBrew = reportSingle.getPicture();


                    }else{
                        Log.d("none", "none");
                    }

                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                    // Getting Post failed, log a message##
                    String s=databaseError.toException().toString();
                    Log.d( "Post:Cancelled", s);

                    Toast.makeText(MealDetailsActivity.this, "Failed to load data.",
                            Toast.LENGTH_SHORT).show();

                }
            });
           }

0 个答案:

没有答案