如何显示存储在数据库中的评级值作为星数
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));
要获得与附加图片一样的输出:
# 如果Restaurant
的number
固定。 (例如:10
):
- 您应该在布局
XML
中添加 10 RatingBar
和 10 TextView
。
- 对 RatingBar
(rb1, rb2, rb3.....)
和 TextView(tv1, tv2, tv3.....)
使用不同的 id
。
从 database
中获取 rating
个值:
Float rating1 = 1.0;
Float rating2 = 1.0;
Float rating3 = 3.0;
.............
.......................
将 rating
值设置为 RatingBar
和 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));
................
.......................
# 如果Restaurant
变量的number
:
- 您应该使用
ListView
或 RecyclerView
。
- 使用
ArrayList
存储您从 Google Map
. 获得的每个餐厅数据(name
和 rating
)
- 创建自定义
Adapter
以填充每个餐厅 data
在 ListView
/RecyclerView
的行项目 view
上。这里的行项目视图是一个 XML 包含一个 RatingBar
和 TextView
.
- 将餐厅
ArrayList
传递给 Adapter
- 最后,从 Adapter 的
getView()
或 onBindViewHolder()
中为每个 position
从 ArrayList
中获取餐厅 name
和 rating
并显示在TextView
和 RatingBar
.
这里有一些很好的教程:
- Implementation of RatingBar
- Android ListView with Custom Adapter
- Android RecyclerView with Custom Adapter
希望对你有所帮助~
我有一个 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));
要获得与附加图片一样的输出:
# 如果Restaurant
的number
固定。 (例如:10
):
- 您应该在布局
XML
中添加 10RatingBar
和 10TextView
。 - 对 RatingBar
(rb1, rb2, rb3.....)
和 TextView(tv1, tv2, tv3.....)
使用不同的id
。 从
database
中获取rating
个值:Float rating1 = 1.0; Float rating2 = 1.0; Float rating3 = 3.0; ............. .......................
将
rating
值设置为RatingBar
和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)); ................ .......................
# 如果Restaurant
变量的number
:
- 您应该使用
ListView
或RecyclerView
。 - 使用
ArrayList
存储您从Google Map
. 获得的每个餐厅数据( - 创建自定义
Adapter
以填充每个餐厅data
在ListView
/RecyclerView
的行项目view
上。这里的行项目视图是一个 XML 包含一个RatingBar
和TextView
. - 将餐厅
ArrayList
传递给Adapter
- 最后,从 Adapter 的
getView()
或onBindViewHolder()
中为每个position
从ArrayList
中获取餐厅name
和rating
并显示在TextView
和RatingBar
.
name
和 rating
)
这里有一些很好的教程:
- Implementation of RatingBar
- Android ListView with Custom Adapter
- Android RecyclerView with Custom Adapter
希望对你有所帮助~