2013-01-07 73 views
0

我想設置評級到位於列表視圖,但無法設置它的評級欄。我卡在這部分,但直到我沒有得到答案。我的評分欄不可點擊。
這裏是我的xml文件的代碼NumberFormatException:在列表視圖中設置評級到評級欄的麻煩

<RatingBar 
    android:id="@+id/ratingBar1" 
    style="?android:attr/ratingBarStyleSmall" 
    android:layout_width="60dp" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/title" 
    android:layout_marginRight="46dp" 
    android:layout_toLeftOf="@+id/imageView1" 
    android:isIndicator="true" 
    android:numStars="5" 
    android:stepSize="0.1" /> 

這裏是我的代碼來設置評級的評價吧。其實我從json文件中獲取價值,然後我把它投入浮動。看這個。

static final String TAG_DISHRATING = "dishrating"; 
.... 
float dishRating = Float.valueOf(dishResult.getString(TAG_DISHRATING));//parse from json file 
..... 
//then i add it i hashmap 
map.put(TAG_DISHRATING, String.valueOf(dishRating)); 
// Then i am getting it form map 
for(HashMap hm:songsList) 
    if(hm.get(TAG_lOCATIONNAME).equals(name)) 
    {      
    dishid = Integer.parseInt(hm.get(TAG_DISHiD).toString()); 
    float dishrating = Float.parseFloat(hm.get(TAG_DISHRATING).toString());      
    } 

最後我設置評級,評級酒吧

RatingBar ratingBar = (RatingBar)vi.findViewById(R.id.ratingBar1);// rating bar 

    ratingBar.setClickable(false); 
    HashMap<String, String> song = new HashMap<String, String>(); 
    song = data.get(position); 

    // Setting all values in listview 
    title.setText(song.get(HotelList.TAG_lOCATIONNAME)); 
    artist.setText(song.get(HotelList.TAG_DISHTOTALVOTES)); 
    ratingBar.setRating(Float.parseFloat(HotelList.TAG_DISHRATING)); //here i am getting exception 

logcat的例外:

01-07 13:10:24.250: E/AndroidRuntime(25565): FATAL EXCEPTION: main 
01-07 13:10:24.250: E/AndroidRuntime(25565): java.lang.NumberFormatException: Invalid float: "dishrating" 
01-07 13:10:24.250: E/AndroidRuntime(25565): at java.lang.StringToReal.invalidReal(StringToReal.java:63) 
01-07 13:10:24.250: E/AndroidRuntime(25565): at java.lang.StringToReal.parseFloat(StringToReal.java:310) 
01-07 13:10:24.250: E/AndroidRuntime(25565): at java.lang.Float.parseFloat(Float.java:300) 
01-07 13:10:24.250: E/AndroidRuntime(25565): at com..LazyAdapter.getView(LazyAdapter.java:57) 

回答

2
ratingBar.setRating(Float.parseFloat(HotelList.TAG_DISHRATING)); 

應該

ratingBar.setRating(Float.parseFloat(song.get(HotelList.TAG_DISHRATING))); 

因爲當前您正試圖將字符串解析爲Float。通過正確的價值Float.parseFloat

+0

我tag_dishrating是字符串。我使用你的代碼,但它顯示類型RatingBar中的方法setRating(float)不適用於參數(String) –

+0

@SandipArmalPatil:使用ratingBar.setRating(Float.parseFloat(song.get(HotelList.TAG_DISHRATING)));行看到我的回答 –

+0

@SandipArmalPatil:通過'dishrating'字符串的值而不是「盤算」 –

1

嘗試

ratingBar.setRating(Float.parseFloat(song.get(HotelList.TAG_DISHRATING)));