首页 > 解决方案 > 数据库写入率

问题描述

我正在尝试为一位医生编写许多用户的比率,当我单击按钮时,比率变量不要写入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);
                }
            });

        }
    }

标签: androidfirebasefirebase-realtime-database

解决方案


推荐阅读