2

How to display rating in the ratingbar based on Firebase? I have successfully uploaded its rating on the Firebase.

Code below shows how I uploaded the rating based on rating bar to the Firebase which works successully.

ratingRef.child("Rating").setValue(ratingstar);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        Toast.makeText(UploadReviewAndRating.this, "Stars: " + (int)rating, Toast.LENGTH_SHORT).show();
    }
});

Intent intent = new Intent(UploadReviewAndRating.this, ViewProfile.class);
startActivity(intent);

Code below shows how I display the rating bar which does not work. When I run the app, the application crashes and displays error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference.

ratingRef.child("Ratings").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int rating = dataSnapshot.getValue(int.class);
        ratingBar.setRating(rating);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
Hakiim
  • 37
  • 1
  • 7
  • [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Grimthorr Oct 09 '17 at 12:14
  • I have check and still does not wok. If possible, can you show an example on displaying rating on rating bar based on firebase? it would be helpful – Hakiim Oct 09 '17 at 13:06
  • You need to check that the `dataSnapshot` in `onDataChange` is not `null` before using it. Also note: you have used `.child("Rating")` to save and then `.child("Ratings")` to load, which are different. – Grimthorr Oct 09 '17 at 13:35
  • Can you edit your question to include the JSON at `ratingRef.child("Ratings")` (as text, no screenshot please). You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Oct 09 '17 at 14:41
  • thanks will try later – Hakiim Oct 11 '17 at 06:02

1 Answers1

2

When onRatingChanged() is called you need to save the result to the database using your ratingRef.

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        if (fromUser) ratingRef.child("Rating").setValue(rating);
    }
});

And then attach a listener to the same ratingRef child to load the rating, while also checking that the returned result from the database (dataSnapshot) is not null.

ratingRef.child("Rating").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot != null && dataSnapshot.getValue() != null) {
            float rating = Float.parseFloat(dataSnapshot.getValue().toString());
            ratingBar.setRating(rating);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) { }
});
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
  • thanks for replying and i am sorry for late updating. I will try later. – Hakiim Oct 11 '17 at 06:02
  • its says cannot resolve method 'valueOf(java.lang.Object)' is there a way? – Hakiim Oct 12 '17 at 09:15
  • Ah sorry, try: `int rating = dataSnapshot.getValue(Integer.class);` instead. – Grimthorr Oct 12 '17 at 10:08
  • it solve the error but it didnt display anything at the rating bar – Hakiim Oct 12 '17 at 11:19
  • I also found this at stackoverflow : Float rating = Float.parseFloat(stringFromDatabase); mRateBar.setRating(rating); textView.setText("Rating: " + stringFromDatabase); what does it mean string from database? – Hakiim Oct 12 '17 at 11:20
  • I assume that would be the result you obtain from the database listener using `dataSnapshot.getValue()`. – Grimthorr Oct 12 '17 at 11:23
  • can you elaborate more? – Hakiim Oct 12 '17 at 12:33
  • Having looked into this some more, I see that the `setRating()` method of the RatingBar only accepts a float value. Please see my updated answer, which saves the rating value to the database as a float and then uses `Float.parseFloat()` to read it again, which should work. – Grimthorr Oct 12 '17 at 13:49
  • it is still doesnt work. However thanks for your time for helping and i am really appreciate it. – Hakiim Oct 12 '17 at 16:33
  • I'm happy to help. Do you receive any further errors? – Grimthorr Oct 12 '17 at 16:36
  • there is no error. The apps still ran without any crash and error. It just that it doesnt display the rating on rating bar. – Hakiim Oct 12 '17 at 16:38
  • 1
    I've done some testing myself and can get it to work reliably - please see my full code example at https://gist.github.com/anonymous/195c491013a447d27c42adf7c281199a. – Grimthorr Oct 13 '17 at 08:58
  • wow! nice finally work! thanks man and i am really appreciate your time and help. +1 – Hakiim Oct 14 '17 at 12:43
  • Glad I could help! Please feel free to upvote my answer. – Grimthorr Oct 14 '17 at 16:58