来自firebase中不同引用的访问值

时间:2018-01-25 13:31:14

标签: android firebase firebase-realtime-database

我在访问不同引用的值时遇到问题。 这是第一部分的代码。

   DatabaseReference databaseBazars.addListenerForSingleValueEvent (new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            Integer mealrate = 0;

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                Bazar bazar = ds.getValue(Bazar.class);
                Integer cost = Integer.valueOf(bazar.getCost());
                mealrate = mealrate + cost; //want to send the value of mealrate


            }




            AlertDialog.Builder dialog1 = new AlertDialog.Builder(ShowMeal.this);
            dialog1.setTitle("Notification");
            dialog1.setMessage("Total Cost  : " +mealrate);
            dialog1.setIcon(R.drawable.ic_done_black_24dp);
            dialog1.setNeutralButton(
                    "OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            dialog.dismiss();

                        }
                    });
            dialog1.show();

        }


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

现在我希望获得"餐饮的价值"在那个事件中(下面)。以它们属于同一个函数的方式。我可以从上层事件到下层事件(dbr.addLis .....)获取值

DatabaseReference dbr.addListenerForSingleValueEvent (new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            abcd.clear();
            Integer total = 0;



            for (DataSnapshot ds : dataSnapshot.getChildren()) {

                Integer costs7=Integer.valueOf(meals2.getS2());
              total = total +cost7+ mealrate;//here i can't get the value of mealrate
                 abcd.add(meals2);


            }




        MealList adapter = new MealList(ShowMeal.this, abcd);
        sm.setAdapter(adapter);

        AlertDialog.Builder dialog1 = new AlertDialog.Builder(ShowMeal.this);
        dialog1.setTitle("Notification");



        dialog1.setMessage("Total Meal  : " +total );


        dialog1.setIcon(R.drawable.ic_done_black_24dp);
        dialog1.setNeutralButton(
                "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.dismiss();

                    }
                });
        dialog1.show();

    }


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

请帮帮我。

我也试过这种方式,它会让应用程序崩溃

   databaseBazars.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {




        dbr.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {


                Integer totala = 0;

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    Bazar bazar = ds.getValue(Bazar.class);
                    Integer cost = Integer.valueOf(bazar.getCost());
                    totala = totala + cost;


                }



                abcd.clear();
                Integer total = 0;
                Integer s1=0;
                Integer s2=0;
                Integer s3=0;
                Integer s4=0;
                Integer s5=0;
                Integer s6=0;
                Integer s7=0;

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    MealE meals1 = ds.getValue(MealE.class);
                    MealE meals2 = ds.getValue(MealE.class);
                    MealE meals3 = ds.getValue(MealE.class);
                    MealE meals4 = ds.getValue(MealE.class);
                    MealE meals5 = ds.getValue(MealE.class);
                    MealE meals6 = ds.getValue(MealE.class);
                    MealE meals7 = ds.getValue(MealE.class);
                    Integer costs1 = Integer.valueOf(meals1.getS1());
                    Integer costs2=Integer.valueOf(meals2.getS2());
                    Integer costs3=Integer.valueOf(meals3.getS3());
                    Integer costs4=Integer.valueOf(meals4.getS4());
                    Integer costs5=Integer.valueOf(meals5.getS5());
                    Integer costs6=Integer.valueOf(meals6.getS6());
                    Integer costs7=Integer.valueOf(meals7.getS7());
                    s1=s1+costs1;
                    s2=s2+costs2;
                    s3=s3+costs3;
                    s4=s4+costs4;
                    s5=s5+costs5;
                    s6=s6+costs6;
                    s7=s7+costs7;

                    total = total + costs1+costs2+costs3+costs4+costs5+costs6+costs7;
                    abcd.add(meals1);

                }


                MealList adapter = new MealList(ShowMeal.this, abcd);
                sm.setAdapter(adapter);

                AlertDialog.Builder dialog1 = new AlertDialog.Builder(ShowMeal.this);
                dialog1.setTitle("Notification");



                dialog1.setMessage("Total Meal  : " +total
                        +"\n" +"\n"+"Nayeem  : " +s1
                        +"\n"+"\n"+"Nizam   : " +s2
                        +"\n"+"\n"+"Rabbani : " +s3
                        +"\n"+"\n"+"Saikat  : " +s4
                        +"\n"+"\n"+"Sagar   : " +s5
                        +"\n"+"\n"+"Nayan   : " +s6
                        +"\n"+"\n"+"Touqir  : " +s7
                        +"\n"+"\n" +"\n"+"MEAL RATE :  "+totala);


                dialog1.setIcon(R.drawable.ic_done_black_24dp);
                dialog1.setNeutralButton(
                        "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                dialog.dismiss();

                            }
                        });
                dialog1.show();



                }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

1 个答案:

答案 0 :(得分:0)

你不能,因为当时听众是一个参考;你可以做的是将引用连接在一起并使用那里的值,看看这个片段。

    mDatabase.child("Users").child(mAuth.getCurrentUser().getUid()).child("files").child("files_"+sharedPrefsDefault.getString(getString(R.string.str_idioma), locale)).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(final DataSnapshot dataSnapshotFiles) {

                mDatabase.child("Users").child(mAuth.getCurrentUser().getUid()).child("filescar").child("filescar_"+sharedPrefsDefault.getString(getString(R.string.str_idioma), locale)).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshotFilesCar) {


            // I do something with the two references,
            // here is your for statement

}

看一下,您从第一个Firebase参考请求数据,您获取数据并将其存储在mealrate,现在如果您从其他参考中访问该变量而没有连接,您可以获得更新的值你的第一个引用和你的第二个引用不会要求更新的值,甚至应用程序都会崩溃,因此,如果你把你的引用放在一起,你可以使用它们中的两个并且没有任何问题。