2012-03-30 5 views
2

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> 

答えて

10

こんにちは、私は次のコードを `........列totalRating = c.getString(TAG_RATINGを)書いたコード

<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

下に確認してください。 RatingBar ratingBar =(RatingBar)findViewById(R.id.pop_ratingbar); \t \t \t \t float totalRatingValue = Float.parseFloat(totalRating); \t \t \t \t ratingBar.setRating(totalRatingValue); HashMap map = new HashMap (); map.put(TAG_NAME、 "名前:" +名前); ................ 'しかし、強制終了エラーが発生します。 – captaindroid

+0

@captainpirate - あなたのTAG_RATINGが浮動小数点に変換可能な場合、評価値を設定する際にエラーがないと思わないので、logcatを表示できますか? –

+0

RatingBarが各リスト項目に表示されますが、 'ratingBar.setRating totalRatingValue); 'それから、私のプログラムは強制的に緊密なエラーを出します。 – captaindroid

0

こんにちは評価バーはRatingBarのオブジェクトを作成し、浮動小数点値と関連する評価を割り当てるアンドロイドsee thisthisthis

6

でもあります。

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