繁体   English   中英

如何在android中显示评级栏?

[英]How to display rating bar in android?

我成功解析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>

您好检查以下代码

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

创建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);

嗨评级栏是在android中看到 这个这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM