[英]How to copy ratingbar value to firebase database android?
我正在为我的学校制作一个简单的应用程序,用户将对该服务进行评分,然后按Submit,它将数据发送到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>
您正在评级更改监听器中设置值,更改评级后将立即触发该值。 在“发送反馈”按钮上添加一个侦听器,然后单击以在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
}
});
要解决此问题,请使用以下代码:
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.