【问题标题】:How to copy ratingbar value to firebase database android?如何将ratingbar值复制到firebase数据库android?
【发布时间】:2018-03-13 08:43:45
【问题描述】:

我正在为我的学校制作一个简单的应用程序,用户将对服务进行评分并按下提交,这会将数据发送到 firebase,但同时会重置评分栏的值,以便下一个用户可以评分再次。但是我遇到了一些困难和误解。请帮忙!这是我的主要活动代码:

    package com.timothy.ratingch;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.hsalf.smilerating.SmileRating;


public class MainActivity extends AppCompatActivity {

    private SmileRating mRatingBar;
    private DatabaseReference mRatingBarCh;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SmileRating smileRating = (SmileRating) findViewById(R.id.ratingBar);
        button = (Button) findViewById(R.id.button);
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        final DatabaseReference mRatingBarCh = rootRef.child("ratings");


        smileRating.setOnRatingSelectedListener(new SmileRating.OnRatingSelectedListener() {
            @Override
            public void onRatingSelected(int level, boolean reselected) {
                Toast.makeText(getApplicationContext(), "Your feedback value is " + level,
                        Toast.LENGTH_SHORT).show();
                mRatingBarCh.child("rating").setValue(String.valueOf(level));

                // reselected is false when user selects different smiley that previously selected one
                // true when the same smiley is selected.
                // Except if it first time, then the value will be false.
            }
        });

    }
}

这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.timothy.ratingch.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="103dp"
        android:text="Please Rate Our Service at CH!"
        android:textAlignment="center"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.hsalf.smilerating.SmileRating
        android:id="@+id/ratingBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="35dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ratingBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:background="#dfeded"
        android:elevation="0dp"
        android:text="SEND FEEDBACK"
        android:textSize="12sp" />

</RelativeLayout>

【问题讨论】:

    标签: java android database firebase firebase-realtime-database


    【解决方案1】:

    要解决这个问题,请使用以下代码:

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            float rating = ratingBar.getRating();
            mRatingBarCh.child("rating").setValue(String.valueOf(rating));
        }
    });
    

    您的代码中的问题是您将view 传递给setValue() 方法,而不是受支持的数据类型。

    并且不要忘记像这样初始化您的 mRatingBarCh 字段:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference mRatingBarCh = rootRef.child("ratings");
    

    【讨论】:

    • 请帮助@AlexMamo
    • 请帮助我@AlexMamo
    • 我会的。有什么问题?
    • 它确实成功发送了数据,但它只覆盖了旧数据@AlexMamo
    • 当我查看 firebase 中的数据时,它只是覆盖了旧数据@AlexMamo
    【解决方案2】:

    您正在评级更改侦听器中设置值,一旦您更改评级,该侦听器就会被触发。 在您的发送反馈按钮上添加一个监听器,然后单击在 firebase 中设置值:

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // get the value of rating from rating bar
            mRatingBarCh.child("rating").setValue(v);
            // reset your rating bar
        }
    });
    

    【讨论】:

    • 我不太明白,先生,您介意解释一下吗?
    • 您说您只想在单击按钮时发送反馈,因此您应该在单击按钮时设置数据库中的值,而不是在评分栏的更改时。
    • 你能教我怎么做吗?我真的是 firdatabase 先生 @UmarHussain 的初学者
    • 请帮助@Umar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多