如何将Ratingbar值复制到Firebase数据库Android? [英] How to copy ratingbar value to firebase database android?

查看:34
本文介绍了如何将Ratingbar值复制到Firebase数据库Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a simple app for my school that the user will rate the service and press submit, which sends the data to firebase, but at the same time, resets the value of the rating bar so the next user can rate again. But I am having some difficulties and misunderstandings. Please Help! Here is my main activity code:

    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>

推荐答案

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

To solve this, please use the following code:

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()方法,而不是受支持的数据类型.

The problem in your code is that you are passing a view to the setValue() method and not a supported data type.

同样不要忘记像下面这样初始化您的 mRatingBarCh 字段:

And also don't forget to intialize your mRatingBarCh field like this:

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

这篇关于如何将Ratingbar值复制到Firebase数据库Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆