2013-02-21 45 views
0

我有一個ListFragment,它只包含我想動態填充的ListView。問題是我想用RatingBar視圖填充它。我所做的是聲明一個RatingBar,然後將其添加到View中。動態添加RatingBar到ListFragment

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    RatingBar rb = new RatingBar(this.getActivity()); 
    rb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

    getListView().addView(rb); 

    return (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false); 
} 

,我得到的錯誤:

java.lang.IllegalStateException: Content view not yet created 

我怎樣才能在每個ListView的線,是一個ListFragment裏面添加的RatingBar?

編輯:好了,所以我改變了我的代碼如下:

RatingBar rb = new RatingBar(getActivity()); 
rb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
rb.setIsIndicator(true); 
rb.setNumStars(5); 

List<RatingBar> lrb = new ArrayList<RatingBar>(); 

for (int i = 0; i < li.size(); i++) 
    lrb.add(rb); 

setListAdapter(new ArrayAdapter<RatingBar>(this.getActivity(), 
       android.R.layout.simple_selectable_list_item, lrb)); 

不過,雖然我沒有收到錯誤,我得到這些結果:

Emulator Results

回答

1

ArrayAdapter不真正方便的顯示比簡單的'TextViews'更多。你必須創建你自己的適配器,可能是通過從簡單的BaseAdapter派生。然後你必須用你的數據填充適配器。它將用正確的視圖更新列表視圖。你可以看看this tutorial