2012-04-06 17 views
1

同じレイアウトの3つの行を表示するために、LinearLayoutでListViewをシミュレートしようとしています。この1つのビューは、評価バーとコンテンツで構成されています。非常に奇妙ですが、すべてのratingBarsは最後に割り当てられた値を受け取ります。で)プログラムでLinearLayoutにビューを追加する

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res/com.weorder.client" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/transparent" 
    android:minHeight="60.0dp" 
    android:orientation="vertical" 
    android:paddingBottom="8.0dp" 
    android:paddingLeft="5.0dp" 
    android:paddingRight="5.0dp" 
    android:paddingTop="8.0dp" > 

    <RelativeLayout 
     android:id="@+id/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <RatingBar 
      android:id="@+id/ratingBarInItem" 
      style="@style/RatingBarSm" 
      android:isIndicator="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="3dp" 
      android:numStars="5" 
      android:stepSize="0.1" /> 

    </RelativeLayout> 

    <TextView 
     android:id="@+id/content" 
     style="@style/Content" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="3dp" 
     android:layout_marginRight="3dp" 
     android:layout_marginTop="3dp" 

     android:textColor="@color/dark_brown" 
     android:textSize="@dimen/text_size_small" /> 

</LinearLayout> 

を私はonActivityCreated(内部setElementsメソッドを呼び出します。

public void setElements(List<Item> elements) { 
    removeAllViews(); 
    for (int i = 0; i < elements.size() && i < 3; i++) { 
     View vi = buildElementView(elements.get(i)); 
     vi.setId(i); 
     addView(vi); 
    } 
} 

private View buildElementView(Item itemElement) { 
    View view = inflater.inflate(R.layout.element_list_item, null, true); 
    // set values 
    View header = view.findViewById(R.id.header); 
    RatingBar ratingInItem = (RatingBar) header.findViewById(R.id.ratingBarInItem); 
    TextView content = (TextView) view.findViewById(R.id.content); 

    content.setText(itemElement.getContent()); 

    ratingInItem.setRating(itemElement.getRating()); 
    return view; 
} 

、これは私が膨らま午前レイアウトです:まず、このののLinearLayoutがちょうどこれらの2つのメソッドを追加する拡張私のカスタムコンポーネントであります断片。フラグメントが始まるとうまく動作しますが、電話機を回転しようとするとアイテムのコンテンツは正しく変更されますが、定格が最後の値になります(定格1,2の3つの要素がある場合、すべての定格のバーに3つの星があります) 。バグですか?

EDIT:これは私のonSaveInstanceMethodです:

@Override 
public void onSaveInstanceState(Bundle outState) { 

    if (elements != null) { 
     outState.putSerializable("elements", elements); 
    } 

    super.onSaveInstanceState(outState); 
} 

おかげ

+0

活動が殺されたときにどのように評価を保存しません? – znat

+0

評価はネットワークから取得され、onsaveinstancestateを使用して保存されます – Matroska

+0

コードのその部分を共有できますか? (オンセーブインスタンス) – znat

答えて

0

私はあなたが問題を引き起こしている間違ったビューでfindViewById()を使用していると思います。

ここには、私にとってうまくやっていることに基づいたサンプルがあります。

LinearLayout ll = (LinearLayout)findViewById(R.id.ll); 
ViewGroup parentGroup = parentGroup = (ViewGroup)ll; 

for (int i = 0; i < 3; i++) 
{ 
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.element_list_item, null, true); 
    RatingBar rBar = (RatingBar)view.findViewById(R.id.ratingBar1); 
    rBar.setProgress(i); 
    parentGroup.addView(view); 
} 

結果には、評価バーの設定方法に応じて0,1,2つの評価バーが表示されます。 新しいビューをビューの親に3回アタッチします。親は私の直線レイアウトで、新しいビューはレーティングバー(またはあなたが選んだもの)です。断片的に保存するため

あなたはonCreateViewであなたの引数を取得し、(必要な場合)、あなたのデータを復元することができ

@Override 
public void onSaveInstanceState(Bundle outState) 
{ 
    super.onSaveInstanceState(outState); 
    getFragmentManager().putFragment(outState, Content.class.getName(), this); 
} 

を使用

Bundle extras = this.getArguments(); 
if(extras != null) 
{ 
    //set arguments here 
} 
関連する問題