2017-04-20 140 views
3

我有一个谷歌地图应用程序,用户可以在他们通过标记选择的餐厅中评分我在sq lite数据库中存储了该评分值,当他们点击视图评分时按钮我在滚动视图中显示字符串的名称和评分值。 现在我想要显示星星的评分值,即用户输入的方式。 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> 
+0

可以使用'RatingBar'为。 –

回答

5

要做到这一点,你可以使用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); 

UPDATE:

从获取:

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. 获得从databaserating值:

    Float rating1 = 1.0;  
        Float rating2 = 1.0;  
        Float rating3 = 3.0; 
        .............  
        ....................... 
    
  4. 套装ratingRatingBarTextView

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

如果numberRestaurant变量:

  1. 您应该使用ListViewRecyclerView
  2. 使用ArrayList来存储从Google Map获得的每个餐厅数据(namerating)。
  3. 创建一个自定义Adapter来填充每个餐厅data在行项目viewListView/RecyclerView。此处的行项目视图是一个包含RatingBarTextView的XML。
  4. 传递餐厅ArrayListAdapter
  5. 最后,从适配器的getView()onBindViewHolder()得到ArrayList餐厅namerating每个position,并显示在TextViewRatingBar

这里有一些很好的教程:

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

希望这将有助于〜

+0

我跟着同一个教程来获得用户评分现在我的问题是如何显示在文本视图,即相应restaurent名称和评级在一起。当单击视图评级时,它将显示数据库中的所有数据 –

+0

是否适合您? – FAT

+0

不,它会显示评分值作为数字,我想显示它像星星 –

0

试试这个:

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

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

雅它的作品,但如何显示在一个文本视图,即对应restaurent名称和评级在一起。点击查看评分时,显示数据库中的所有数据。 –

+0

您可以发布一些图片或其他您想要的内容。 –

+0

已发布图像仅供参考 –

相关问题