How to display the estimated value that is stored in the database as the number of stars

I have a google maps app where users can rate in a restaurant they choose using a marker i that stores this rating value in sqlite database and when they click the view rating button I show the name and rating value as a string in the scrollview. Now I want to show this rating value as stars, that is, the way user input. this is how i want to display when the 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>

      

+3


source to share


2 answers


For 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 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 the result as your attached image:

# If fixed number

of Restaurant

. (For example :) 10

:

  • You XML

    should add 10 RatingBar

    and 10 in your layout TextView

    .
  • Use different id

    for RatingBar (rb1, rb2, rb3.....)

    and TextView (tv1, tv2, tv3.....)

    .
  • Get rating

    values ​​from database

    :

       Float rating1 = 1.0;    
       Float rating2 = 1.0;    
       Float rating3 = 3.0;   
       .............    
       .......................
    
          

  • Set the rating

    values RatingBar

    andTextView

       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 variable is a number

variable Restaurant

:

  • You must use ListView

    or RecyclerView

    .
  • Use ArrayList

    (for storage of each restaurant name

    and rating

    ) that you get from Google Map

    .
  • Create a custom Adapter

    one to fill each restaurant data

    on the line view

    at ListView

    / RecyclerView

    . Here, the row element view is XML containing RatingBar

    and TextView

    .
  • Go to the restaurant ArrayList

    beforeAdapter

  • Finally, from adapter getView()

    or onBindViewHolder()

    run name

    and rating

    from ArrayList

    for each position

    and point to TextView

    and RatingBar

    .

Here are some good lessons:

Hope this helps ~

+5


source


Try the following:



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

textView.setText("Rating: " + stringFromDatabase);

      

0


source







All Articles