2012-10-24 37 views
0

我在我的android應用程序中有一個ListAdapter ...我試圖添加一個ratingBar到列表中的每個項目,其值設置爲包含在[TAG_RATING ]。問題在於列表顯示,唯一的ratingBar卡在列表的最底部,而且相當大。添加RatingBar到ListAdapter使用簡單的適配器不能正常工作

請參閱下面的代碼:

ListAdapter adapter = new SimpleAdapter(
         AllProductsActivity.this, productList, 
         R.layout.list_item, new String[] { TAG_BID, 
           TAG_NAME, 
           TAG_RATING}, 
         new int[] { R.id.pid, R.id.name, R.id.ratingBar}); 
        ((SimpleAdapter) adapter).setViewBinder(new MyBinder()); 
       // updating listview 
       setListAdapter(adapter); 

那麼ViewBinder類看起來是這樣的:

class MyBinder implements ViewBinder{ 
    public boolean setViewValue(View view, Object data, String textRepresentation) { 
     if(view.getId() == R.id.ratingBar){ 
      String stringval = (String) data; 
      float ratingValue = Float.parseFloat(stringval); 
      RatingBar ratingBar = (RatingBar) view; 
      ratingBar.setRating(ratingValue); 
      return true; 
     } 
     return false; 
    } 
} 

並且此列表項的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/pid" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:visibility="gone" /> 

<!-- Name Label --> 
<TextView 
    android:id="@+id/name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="6dip" 
    android:paddingLeft="6dip" 
    android:textSize="17dip" 
    android:textStyle="bold" /> 

    <RatingBar 
    android:id="@+id/rating" 
    style="?android:attr/ratingBarStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="6dip" 
    android:stepSize="0.25" 
    android:numStars="5" 
    /> 

回答

1

RatingBar小部件應放置在適配器R.layout.list_item中使用的列表行佈局中。現在,您只需在主佈局中的ListView下放置一個RatingBar小部件。

+0

我剛剛發現,幾分鐘前,我已經改變它.. – Skindeep2366

+0

是的,這是它,但我的評級欄是巨大的... IsIndicator =「真」是我正在使用,但他們仍然是大.. – Skindeep2366

+1

@ Skindeep2366你應該發佈行佈局文件。如果刪除'isIndicator'屬性,'RatingBar'會變小嗎? – Luksprog