简体   繁体   English

如何将星级插入到firebase

[英]How to insert the star rating into firebase

I have the following code and I can only get it to work logically if I set the star rating to hidden first and then let the user rate the star after they have given the review.我有以下代码,如果我先将星级设置为隐藏,然后让用户在他们给出评论后对星级进行评分,我才能使其合乎逻辑地工作。 I am not sure how it works but the rating of the star only gets inserted into the database if the user click on the star.我不确定它是如何工作的,但只有当用户点击星星时,星星的评级才会插入到数据库中。 I tried to put this under the setOnClickListener for my button, so that the rating score should be sent together with the review but it doesn't work.我试图将它放在我的按钮的 setOnClickListener 下,以便评分应该与评论一起发送,但它不起作用。 I have to do this outside my button....我必须在我的按钮外执行此操作....

add_review_button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        final DatabaseReference reference = FirebaseDatabase.getInstance("https://medical-review-in-australia.firebaseio.com/").getReference();
        reference.addValueEventListener(new ValueEventListener() {


            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot childSnapshot : snapshot.getChildren()) {

                    String clinic_info = String.valueOf(childSnapshot.child("Name").getValue());
                    final String txt_review = review.getText().toString().trim();
                    String txt_dorevitch = dorevitch.getText().toString().trim();
                    String txt_skin_cancer = skin_cancer_check.getText().toString().trim();
                    String txt_ear_suctioning = ear_suctioning.getText().toString().trim();
                    String user_email = user.getText().toString().trim();
                    String user_email2 = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getEmail();
                    String other = others.getText().toString().trim();
                    String input = input_others.getText().toString().trim();

                    //   replace(":" ,"");
                    //    message[0] = message[0].replaceAll("[0-9]" ,"");
                    //   message[0] = message[0].replace("Shop" ,"");
                    //   if (clinic_info.equals(message[0]) && !user_email.isEmpty() && user_email.equals(user_email2) && !txt_review.isEmpty()) {
                        user_email = user_email.split("\\s*@\\s*")[0];
                        final HashMap<String, Object> map = new HashMap<>();
                        map.put("Review by " + user_email, txt_review);
                        FirebaseDatabase.getInstance("https://medical-review-in-australia.firebaseio.com/").getReference().child(message[0]).child("Reviews").updateChildren(map);
                        // rating bar code

                        ratingbar.setVisibility(View.VISIBLE);
                        final String finalUser_email = user_email;
                        ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

                            @Override
                            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                                int rate = (int) rating;
                                String info = null;

                                myRating = (int) ratingBar.getRating();

                                switch (rate) {
                                    case 1:
                                        info = "Sorry to hear that! :( ";
                                        break;
                                    case 2:
                                        info = "We always accept suggestions!";
                                        break;
                                    case 3:
                                        info = "Good enough!";
                                        break;
                                    case 4:
                                        info = "Great! Thanks you!";
                                        break;
                                    case 5:
                                        info = "Awesome! You are the best!";
                                        break;
                                }

                                Toast.makeText(Add_Review.this, info, Toast.LENGTH_SHORT).show();


                                HashMap<String, Object> map5 = new HashMap<>();
                                map5.put("Rating by " + finalUser_email, rate);



                                Toast.makeText(Add_Review.this, "Review Successfully Submited", Toast.LENGTH_SHORT).show();

                                FirebaseDatabase.getInstance("https://medical-review-in-australia.firebaseio.com/").getReference().child(message[0]).child("Rating").updateChildren(map5);

                            }
                        });

I think the reason your rating is saved only when user clicks the star is because the updateChildren code is inside the onRatingChangeListener.我认为只有当用户单击星星时才保存您的评分的原因是因为 updateChildren 代码位于 onRatingChangeListener 中。

Is is not working if you keep updateChildren command outside the the onRatingChangeListener and inside the review button onClickListener?如果您将 updateChildren 命令保留在 onRatingChangeListener 之外和 onClickListener 审核按钮内,是否不起作用?

Also try this :也试试这个:

  1. Keep the onRatingChangeListener outside the review Button;s OnclickListener.将 onRatingChangeListener 保留在评论按钮之外;s OnclickListener。

  2. Declare the info variable as public so that it can be used in both functions ie OnRatingChangeListener ( which will save rating in the info variable ) and then the button's OnclickListener ( which will save the rating to database using info variable)info变量声明为 public 以便它可以在两个函数中使用,即 OnRatingChangeListener (它将在info变量中保存评级),然后是按钮的 OnclickListener (它将使用info变量将评级保存到数据库中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM