简体   繁体   中英

How to display rating bar in android?

I am successful to parse JSON and display in listview.
In list view I displayed name, town and rating, which valued are already stored in database. I want to display the rating bar instead of rating number there. Also it should not support any user interaction, I mean I want to display rating bar only for information.
How can i do this ??
Help !!

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

Hello Check below code

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

Create an object of RatingBar and assign the relevant ratings with float value.

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

Here you are getting in String, so you have to type cast to float.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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