Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. package com.example.root.profile;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.RatingBar;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. import com.google.firebase.database.DataSnapshot;
  13. import com.google.firebase.database.DatabaseError;
  14. import com.google.firebase.database.DatabaseReference;
  15. import com.google.firebase.database.FirebaseDatabase;
  16. import com.google.firebase.database.Query;
  17. import com.google.firebase.database.ValueEventListener;
  18.  
  19. import static android.R.attr.key;
  20. import static android.R.attr.value;
  21. import static com.example.root.profile.R.id.view;
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25. private TextView txtRatingValue;
  26. private RatingBar ratingBar;
  27. private static final String TAG = "Rating";
  28. private Button btnSubmit;
  29. int i = 0;
  30. float total = 0;
  31. float average;
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36.  
  37.  
  38. FirebaseDatabase database = FirebaseDatabase.getInstance();
  39. final DatabaseReference rateRes = database.getReference("rating").child("Ahmed");
  40. // final Query name = rateRes.equalTo("Ahmed");
  41. // Read from the database
  42.  
  43. rateRes.addValueEventListener(new ValueEventListener() {
  44. // On Data Change
  45. @Override
  46. public void onDataChange(DataSnapshot dataSnapshot) {
  47. // This method is called once with the initial value and again
  48. // whenever data at this location is updated.
  49. // if(name != null){
  50. // //Object rate = dataSnapshot.getValue();
  51. // Object rate = 3.5f;
  52. // ratingBar.setRating((Float) rate);
  53. // }
  54.  
  55. addListenerOnRatingBar();
  56. Float value = dataSnapshot.getValue(Float.class);
  57. if(value == null){
  58. value = Float.valueOf(0);
  59. }
  60. txtRatingValue.setText(String.valueOf(value));
  61. final Float finalValue = value;
  62. findViewById(R.id.ratebtn).setOnClickListener(new View.OnClickListener(){
  63. @Override
  64. public void onClick(View view) {
  65.  
  66. addListenerOnButton();
  67. total += finalValue;
  68. average = total/++i;
  69. rateRes.setValue(String.valueOf(average));
  70. }
  71. });
  72.  
  73.  
  74. Log.d(TAG, "Value is: " + value);
  75.  
  76. }
  77.  
  78. @Override
  79. public void onCancelled(DatabaseError error) {
  80. // Failed to read value
  81. Log.w(TAG, "Failed to read value.", error.toException());
  82. }
  83. });
  84. }
  85.  
  86.  
  87. public void addListenerOnRatingBar() {
  88.  
  89. ratingBar = (RatingBar) findViewById(R.id.ratingBar);
  90. txtRatingValue = (TextView) findViewById(R.id.txtRatingValue);
  91.  
  92. //if rating value is changed,
  93. //display the current rating value in the result (textview) automatically
  94. ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
  95. public void onRatingChanged(RatingBar ratingBar, float rating,
  96. boolean fromUser) {
  97.  
  98. txtRatingValue.setText(String.valueOf(rating));
  99.  
  100. }
  101. });
  102. }
  103. public void addListenerOnButton() {
  104.  
  105. ratingBar = (RatingBar) findViewById(R.id.ratingBar);
  106. btnSubmit = (Button) findViewById(R.id.ratebtn);
  107.  
  108. //if click on me, then display the current rating value.
  109. btnSubmit.setOnClickListener(new View.OnClickListener() {
  110.  
  111. @Override
  112. public void onClick(View v) {
  113.  
  114. Toast.makeText(MainActivity.this,
  115. String.valueOf(ratingBar.getRating()),
  116. Toast.LENGTH_SHORT).show();
  117.  
  118. }
  119.  
  120. });
  121.  
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement