【问题标题】:How to display rating bar in android?如何在android中显示评分栏?
【发布时间】:2012-03-30 10:28:13
【问题描述】:

我成功解析 JSON 并显示在列表视图中。
在列表视图中,我显示了名称、城镇和评级,这些值已经存储在数据库中。 我想在那里显示评级栏而不是评级数字。 它也不应该支持任何用户交互,我的意思是我只想显示评分栏以获取信息。
我该怎么做??
救命!!

        // 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>

【问题讨论】:

    标签: android rating


    【解决方案1】:

    你好检查下面的代码

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

    【讨论】:

    • 我写了下面的代码........ String totalRating = c.getString(TAG_RATING); RatingBar ratingBar = (RatingBar) findViewById(R.id.pop_ratingbar); float totalRatingValue=Float.parseFloat(totalRating); ratingBar.setRating(totalRatingValue); HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put(TAG_NAME, "Name: " + name); .................... 但是给出了强制关闭错误。
    • @captainpirate -- 你能显示你的 logcat,因为如果你的 TAG_RATING 可转换为浮动,我认为在设置评级值时不会出错..
    • RatingBar 显示到每个列表项,但是当我添加 ratingBar.setRating(totalRatingValue); 然后我的程序给出强制关闭错误。
    • logcat: 04-04 16:48:42.041: D/AndroidRuntime(1971): Shutting down VM 04-04 16:48:42.041: W/dalvikvm(1971): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 04-04 16:48:42.120: D/dalvikvm(1971): GC_FOR_MALLOC freed 6241 objects / 236960 bytes in 75ms 04-04 16:48:42.140: E/AndroidRuntime(1971): FATAL EXCEPTION: main 04-04 16:48:42.140: E/AndroidRuntime(1971): java.lang.RuntimeException: Unable to start activity...........................................
    • @captainpirate 请再次检查 totalRating 值可能是在转换值时出错尝试调试代码也最好将代码粘贴到 pastebin 中以便我可以检查它。根据您的代码,我没有发现任何问题。
    【解决方案2】:

    创建一个 RatingBar 对象,并为相关评分分配浮点值。

    RatingBar ratingBar = null;
    ratingBar.setRating(4.0f); // put the rating number you are receiving through json here.
    

    这里你输入的是字符串,所以你必须输入 cast to float。

         String totalRating = c.getString(TAG_RATING);
         Float.parseFloat(totalRating);
    

    【讨论】:

      【解决方案3】:

      你好评分栏在 android see this this 和 this 中有

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-23
        • 2016-06-06
        相关资源
        最近更新 更多