使用Android

时间:2019-05-24 16:07:49

标签: android firebase firebase-realtime-database

我是初学者;我有一些问题想添加个人人数和总费用的评分点,最初的人员编号为1,total_rating为零。我要添加评分,以更新总费用,此数据应根据配方ID保存。但这不是我想做的。请检查并帮助我。

代码是:

rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
        {

            @Override
            public void onRatingChanged(RatingBar ratingBar, final float rating, boolean fromUser)
            {


               // Toast.makeText(DetailActivity.this,"Recipe id is:   "+r_id,Toast.LENGTH_SHORT).show();
                ref= FirebaseDatabase.getInstance().getReference().child("Rating points").child(r_id);
                //add rates
//                total_rates=total_rates+rating;
//                person_number++;

                Rating rating_model=new Rating(total_rates,person_number,rating);
                ref.child(r_id).setValue(rating_model);
                Toast.makeText(DetailActivity.this,"Thanks for your feedback",Toast.LENGTH_SHORT).show();
                rb.setEnabled(false);

                ref.addValueEventListener(new ValueEventListener()
                {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                    {
                        // retrieve rates
                        float R_total_rates= Float.parseFloat(dataSnapshot.child(r_id).child("total_number").getValue().toString());
                        int R_person_number= Integer.parseInt(dataSnapshot.child(r_id).child("person_number").getValue().toString());

                        //update rates
                        points=rating;
                       total_rates=R_total_rates+points;
                      person_number=R_person_number+1;
                        points=rating;

                        Toast.makeText(DetailActivity.this,"Retrieved   /n points  "+points+"Total rates  "+total_rates+"person number"+person_number,Toast.LENGTH_SHORT).show();

                    }


                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError)
                    {

                    }
                });


                //average
                float avg=0;
                avg=total_rates/person_number;
                Toast.makeText(DetailActivity.this,"Average rate is :  "+avg,Toast.LENGTH_SHORT).show();
            }
        });

0 个答案:

没有答案