如何显示存储在数据库中的评级值作为星数

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

我有一个 google 地图应用程序,用户可以在其中通过标记对他们 select 的餐厅进行评分,我将该评分值存储在 sql lite 数据库中,当他们单击查看评分按钮时,我会显示名称和评分值作为滚动视图中的字符串。 现在我想将该评级值显示为星级,即用户输入的方式。

<?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>

试试这个:

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

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

为此,您可以使用 RatingBar。参见 documentation

在你的布局中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" />

在你的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);

更新:

RatingBar得到rating:

Float rating = ratingBar.getRating();

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

要获得与附加图片一样的输出:

# 如果Restaurantnumber固定。 (例如:10):

  1. 您应该在布局 XML 中添加 10 RatingBar 和 10 TextView
  2. 对 RatingBar(rb1, rb2, rb3.....) 和 TextView(tv1, tv2, tv3.....) 使用不同的 id
  3. database 中获取 rating 个值:

       Float rating1 = 1.0;    
       Float rating2 = 1.0;    
       Float rating3 = 3.0;   
       .............    
       .......................
    
  4. rating 值设置为 RatingBarTextView

       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));
       ................
       .......................
    

# 如果Restaurant变量的number

  1. 您应该使用 ListViewRecyclerView
  2. 使用 ArrayList 存储您从 Google Map.
  3. 获得的每个餐厅数据(namerating
  4. 创建自定义 Adapter 以填充每个餐厅 dataListView/RecyclerView 的行项目 view 上。这里的行项目视图是一个 XML 包含一个 RatingBarTextView.
  5. 将餐厅 ArrayList 传递给 Adapter
  6. 最后,从 Adapter 的 getView()onBindViewHolder() 中为每个 positionArrayList 中获取餐厅 namerating 并显示在TextViewRatingBar.

这里有一些很好的教程:

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

希望对你有所帮助~