2012-03-30 1 views
2

Мне удалось разобрать JSON и отобразить его в списке.
В окне списка отображено имя, город и рейтинг, которые уже хранятся в базе данных. Я хочу отобразить рейтинговую таблицу вместо номера рейтинга. Также он не должен поддерживать какое-либо взаимодействие с пользователем, я имею в виду, что я хочу отображать панель оценки только для информации.
Как я могу это сделать ??
Помощь!Как отобразить панель рейтинга в android?

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

ответ

10

Здравствуйте Проверьте ниже код

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

Я написал следующий код '........ Строка totalRating = c.getString (TAG_RATING); РейтингBar ratingBar = (RatingBar) findViewById (R.id.pop_ratingbar); \t \t \t \t float totalRatingValue = Float.parseFloat (totalRating); \t \t \t \t оценкаBar.setRating (totalRatingValue); HashMap map = new HashMap (); map.put (TAG_NAME, «Имя:» + имя); .................... ', но дает силовую ошибку. – captaindroid

+0

@captainpirate - Вы можете показать свой логарифм, поскольку я не думаю, что есть ошибка при установке значения рейтинга, если ваш TAG_RATING конвертируется в float .. –

+0

RatingBar отображается каждому элементу списка, но когда я добавляю 'ratingBar.setRating (totalRatingValue); 'Тогда мои программы дают сильную ошибку. – captaindroid

6

Создайте объект RatingBar и назначить соответствующие рейтинги со значением с плавающей точкой.

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

Здесь вы попадаете в String, поэтому вам нужно набирать cast для поплавка.

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