我成功解析了JSON并在listview中显示 在列表视图中,我显示了名称,城镇和评级,其中已经存储在数据库中。 我想在那里显示评级栏而不是评级号。 它也不应该支持任何用户交互,我的意思是我只想显示评级栏以获取信息 我怎么能这样做? 帮助!!
// Getting Array of Root
root = json.getJSONArray(TAG_ROOT);
// looping through All root
for (int i = 0; i < root.length(); i++) {
JSONObject c = root.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String town = c.getString(TAG_TOWN);
String totalRating = c.getString(TAG_RATING);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME, "Name: " + name);
map.put(TAG_TOWN, "Town: " + town);
map.put(TAG_RATING, "Rating: " + totalRating);
// adding HashList to ArrayList
myList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, myList,
R.layout.list_item, new String[] { TAG_NAME, TAG_TOWN,
TAG_RATING }, new int[] { R.id.name, R.id.address,
R.id.rating });
setListAdapter(adapter);
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#006fa8"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="#acacac" >
</TextView>
<TextView
android:id="@+id/rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="#acacac" >
</TextView>
</LinearLayout>
答案 0 :(得分:11)
Hello检查以下代码
<RatingBar
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:isIndicator="true"
android:numStars="5"
android:stepSize="1"
android:rating="5"
android:id="@+id/pop_ratingbar"
style="?android:attr/ratingBarStyleSmall"
/>
RatingBar ratingBar = (RatingBar) findviewbyid(R.id.pop_ratingbar);
ratingBar.setRating(4.0f);
答案 1 :(得分:6)
创建RatingBar的对象并使用浮点值指定相关评级。
RatingBar ratingBar = null;
ratingBar.setRating(4.0f); // put the rating number you are receiving through json here.
在这里你得到了String,所以你必须输入cast to float。
String totalRating = c.getString(TAG_RATING);
Float.parseFloat(totalRating);
答案 2 :(得分:0)