我正在尝试将评级设置为位于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文件中获取值,然后我将它转换为float。看这个。
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)
答案 0 :(得分:2)
ratingBar.setRating(Float.parseFloat(HotelList.TAG_DISHRATING));
应该是
ratingBar.setRating(Float.parseFloat(song.get(HotelList.TAG_DISHRATING)));
因为当前您正在尝试将字符串解析为Float。将正确的值传递给Float.parseFloat
答案 1 :(得分:1)
尝试
ratingBar.setRating(Float.parseFloat(song.get(HotelList.TAG_DISHRATING)));