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>