数据库写入率

时间:2018-11-09 17:38:10

标签: android firebase firebase-realtime-database

我正在尝试为一位医生写很多用户的费率,当我单击按钮 Rate 变量不写在 firebase 数据库中时,该值为null任何机构都知道为什么变量在我声明为全局变量时为null

public class RatingProvider extends AppCompatActivity {
        private Button rate;
        private RatingBar bar;
        private TextView providerName, fees;
        private DatabaseReference myRef,ref;
        private String ProvName, ProvID, ProvFees;
        private float barRate;
        private int count;
        private float newRate;
        private static  Rating Rate;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_rating_provider);
            rate = (Button) findViewById(R.id.submit);
            bar = (RatingBar) findViewById(R.id.ratingBar);
            providerName = (TextView) findViewById(R.id.prov);
            fees = (TextView) findViewById(R.id.provFee);
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            ProvName = prefs.getString("provName", "");
            ProvID = prefs.getString("provID", "");
            ProvFees = prefs.getString("Fees", "");
            myRef = FirebaseDatabase.getInstance().getReference("Rating").child(ProvID);
            ref = FirebaseDatabase.getInstance().getReference("Rating").child(ProvID);

            providerName.setText(ProvName);
            fees.setText(ProvFees);
            bar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                    barRate = v;
                }
            });

            rate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    myRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (!dataSnapshot.exists()) {
                                 Rate = new Rating(1, barRate);
                               ref.setValue(Rate);



                            } else {
                                Rating r = dataSnapshot.getValue(Rating.class);
                                newRate = ((int) r.getCounter() * ((float) r.getRate()) + barRate) / ((int) r.getCounter() + 1);
                                count = (int) r.getCounter() + 1;
                                 Rate = new Rating(count, newRate);
                               ref.setValue(Rate);


                            }

                        }

                        @Override
                        public void onCancelled(DatabaseError error) {
                            // Failed to read value
                            Log.w("", "Failed to read value.", error.toException());
                        }
                    });

                    Intent intent = new Intent(getApplicationContext(), SignIn.class);
                    startActivity(intent);
                }
            });

        }
    }

0 个答案:

没有答案