Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.root.profile;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.RatingBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.Query;
- import com.google.firebase.database.ValueEventListener;
- import static android.R.attr.key;
- import static android.R.attr.value;
- import static com.example.root.profile.R.id.view;
- public class MainActivity extends AppCompatActivity {
- private TextView txtRatingValue;
- private RatingBar ratingBar;
- private static final String TAG = "Rating";
- private Button btnSubmit;
- int i = 0;
- float total = 0;
- float average;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- final DatabaseReference rateRes = database.getReference("rating").child("Ahmed");
- // final Query name = rateRes.equalTo("Ahmed");
- // Read from the database
- rateRes.addValueEventListener(new ValueEventListener() {
- // On Data Change
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- // This method is called once with the initial value and again
- // whenever data at this location is updated.
- // if(name != null){
- // //Object rate = dataSnapshot.getValue();
- // Object rate = 3.5f;
- // ratingBar.setRating((Float) rate);
- // }
- addListenerOnRatingBar();
- Float value = dataSnapshot.getValue(Float.class);
- if(value == null){
- value = Float.valueOf(0);
- }
- txtRatingValue.setText(String.valueOf(value));
- final Float finalValue = value;
- findViewById(R.id.ratebtn).setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View view) {
- addListenerOnButton();
- total += finalValue;
- average = total/++i;
- rateRes.setValue(String.valueOf(average));
- }
- });
- Log.d(TAG, "Value is: " + value);
- }
- @Override
- public void onCancelled(DatabaseError error) {
- // Failed to read value
- Log.w(TAG, "Failed to read value.", error.toException());
- }
- });
- }
- public void addListenerOnRatingBar() {
- ratingBar = (RatingBar) findViewById(R.id.ratingBar);
- txtRatingValue = (TextView) findViewById(R.id.txtRatingValue);
- //if rating value is changed,
- //display the current rating value in the result (textview) automatically
- ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
- public void onRatingChanged(RatingBar ratingBar, float rating,
- boolean fromUser) {
- txtRatingValue.setText(String.valueOf(rating));
- }
- });
- }
- public void addListenerOnButton() {
- ratingBar = (RatingBar) findViewById(R.id.ratingBar);
- btnSubmit = (Button) findViewById(R.id.ratebtn);
- //if click on me, then display the current rating value.
- btnSubmit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this,
- String.valueOf(ratingBar.getRating()),
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement