0

我正在尝试将评级设置为位于 Listview 但无法设置的 Ratingbar。我被困在这部分但直到我没有得到答案。我的评分栏不可点击。
这是我的xml文件代码

<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   

日志异常:

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)
4

2 回答 2

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

应该

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

因为当前您正在尝试将字符串解析为浮点数。将正确的值传递给Float.parseFloat

于 2013-01-07T07:44:36.067 回答
1

尝试

ratingBar.setRating(Float.parseFloat(song.get(HotelList.TAG_DISHRATING)));
于 2013-01-07T07:45:52.543 回答