2012-04-03 64 views
2

我得到了以下問題:在我的Android應用程序中,我使用表來顯示一些信息。該信息還包含顯示爲複選框的2個布爾值。當表格被加載時,複選框按照他們應該的方式工作,但只要我改變界面方向,即使數據值爲假,也會檢查兩個複選框。Android - 奇怪的複選框行爲

表的定義是這樣的:

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/table_properties" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#ffffffff" 
/> 

和一個布爾值列在表:

<TableRow 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
> 
    <TextView 
     xmlns:android="http://schemas.android.com/apk/res/android"  
     android:id="@+id/text_jobprop_name" 
     style="@style/text_list_black"  
     android:layout_height="fill_parent" 
     android:textSize="12dp"   
     android:padding="3dp" 
     android:singleLine="false" 
     android:gravity="center_vertical" 
     android:background="@color/table_property_name_background" 
     android:layout_weight="0.7"  
     android:layout_width="0dp" 
    /> 
    <LinearLayout 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:layout_marginRight="5dp" 
     android:layout_marginLeft="10dp" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:background="@color/white"> 
     <CheckBox 
      xmlns:android="http://schemas.android.com/apk/res/android"  
      android:id="@+id/chckbox_jobprop_value" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:clickable="false" 
      android:layout_marginLeft="10dp"      
     /> 
    </LinearLayout> 
</TableRow> 

而且在我調用一個函數活動的onCreate方法是將所有的表格的值。我知道每次更改界面方向時都會調用此函數,但我調試了它並且數據值不會更改。

任何提示,想法,什麼?

謝謝。

回答

3

而在活動的onCreate方法中,我調用了一個函數,該函數爲該表設置了所有值 。我知道每次更改接口方向時都會調用該函數,但我調試了它,並且 數據值不會更改。

這是真的,但請嘗試將複選框的設置放在onResume方法中......如果不是,您的super.onCreate何時被調用?

+0

謝謝,那工作。 – Pathos 2012-04-03 07:42:12

+0

千謝謝,這也爲我解決了:)我聽到其他地方onRestoreInstanceState也可能工作。仍然 - 奇怪的行爲,很難搜索:/ – Groxx 2013-01-10 06:05:31

1

嘗試這樣的..

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
// Save UI state changes to the savedInstanceState. 
// This bundle will be passed to onCreate if the process is 
// killed and restarted. 
savedInstanceState.putBoolean("MyBoolean1",cb1.isChecked()); 
savedInstanceState.putBoolean("MyBoolean2",cb2.isChecked()); 
    super.onSaveInstanceState(savedInstanceState); 
} 

和的onCreate得到這樣的價值觀..

@Override 
public void onCreate(Bundle savedInstanceState) { 
..... 
boolean myBoolean1 = savedInstanceState.getBoolean("MyBoolean1"); 
// now check its value and put the value in your checkbox.. 

現在它會如預期,我認爲......