Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display rating value that is stored in database as number of stars

Tags:

android

rating

i have a google map app where users can rate on the restaurant they select via marker i stored that rating value in sq lite database and when they click on view rating button i display the name and rating value as string in the scrollview. Now i want to show that rating value as stars i.e the way the user enters. this is how i want to display when view rating is clicked

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:text="Rate me"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:id="@+id/rateme"
    android:layout_weight="0.00" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Restaurent Name"
    android:ems="10"
    android:id="@+id/place"
    android:layout_weight="0.00" />
<RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"
    android:rating="2.0"
    android:layout_marginTop="64dp"
    android:layout_below="@+id/place"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"

    android:ems="10"
    android:id="@+id/rate"
    android:layout_weight="0.00" />

<Button
    android:text="Submit"
    android:layout_width="395dp"
    android:layout_height="wrap_content"
    android:id="@+id/submit"
    android:onClick="insert"/>

<Button
    android:text="view Rating"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/viewrate"
    android:layout_weight="0.00"
    android:onClick="display"/>
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button2" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >




        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textStyle="bold" />
        <RatingBar
            android:id="@+id/rat"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:isIndicator="false"
            android:layout_weight="0.07" />
        />




    </LinearLayout>
</ScrollView>

 </LinearLayout>
like image 808
Vidhya Sampath Avatar asked Apr 20 '17 05:04

Vidhya Sampath


People also ask

How do you value a rating bar?

Get Android RatingBar Value In android, by using RatingBar methods (getNumStars(), getRating()) we can get the number of stars and the rating value which was selected. Following is the code snippet to get the rating details from RatingBar in android applications. int noofstars = rBar. getNumStars();


2 Answers

To do this, you can use RatingBar. See documentation.

In your layout XML:

<RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"
    android:rating="2.0" />

In your Activity:

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);


Float rating = 3.0; // 3.0 is from your database

// To show rating on RatingBar
ratingBar.setRating(rating);

UPDATE:

To get rating from RatingBar:

Float rating = ratingBar.getRating();

// Show rating on TextView
textView.setText("Rating: " + String.valueOf(rating));

To get output as like your attached image:

# If the number of Restaurant fixed. (For example: 10):

  1. You should add 10 RatingBar and 10 TextView in your layout XML.
  2. Use different id for RatingBar(rb1, rb2, rb3.....) and TextView(tv1, tv2, tv3.....).
  3. Get rating values from database:

       Float rating1 = 1.0;    
       Float rating2 = 1.0;    
       Float rating3 = 3.0;   
       .............    
       .......................
    
  4. Set rating values to RatingBar and TextView

       rb1.setRating(rating1);
       rb2.setRating(rating2);
       rb3.setRating(rating3);
       ................
       ......................
    
       tv1.setText("Restaurant One" + String.valueOf(rating1));
       tv2.setText("Restaurant Two" + String.valueOf(rating2));
       tv3.setText("Restaurant Three" + String.valueOf(rating3));
       ................
       .......................
    

# If the number of Restaurant variable:

  1. You should use ListView or RecyclerView.
  2. Use an ArrayList to store each restaurant data (name and rating) that you get from Google Map.
  3. Create a custom Adapter to populate each restaurant data on row item view on ListView/RecyclerView. Here row item view is a XML containing a RatingBar and TextView.
  4. Pass the restaurant ArrayList to Adapter
  5. Finally, from Adapter's getView() or onBindViewHolder() get the restaurant name and rating from ArrayList for each position and show on TextView and RatingBar.

Here are some good Tutorials:

  1. Implementation of RatingBar
  2. Android ListView with Custom Adapter
  3. Android RecyclerView with Custom Adapter

Hope this will help~

like image 108
Ferdous Ahamed Avatar answered Sep 19 '22 01:09

Ferdous Ahamed


Try This:

Float rating = Float.parseFloat(stringFromDatabase);
mRateBar.setRating(rating);

textView.setText("Rating: " + stringFromDatabase);
like image 39
Sai Avatar answered Sep 18 '22 01:09

Sai