2017-04-20 117 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

已發佈圖像僅供參考 –

相關問題