问题
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.
}
});
}
}
and here is my xml code:
<?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>
回答1:
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));
}
});
The problem in your code is that you are passing a view
to the setValue()
method and not a supported data type.
And also don't forget to intialize your mRatingBarCh
field like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mRatingBarCh = rootRef.child("ratings");
回答2:
You are setting value inside rating change listener which will gets triggered as soon as you change the rating. Add a listener on your send feedback button and then on click set the value in 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
}
});
来源:https://stackoverflow.com/questions/49251669/how-to-copy-ratingbar-value-to-firebase-database-android