2013-08-28 45 views
0

我想知道爲什麼我的複選框不會將狀態從false更改爲true。android複選框狀態不變

我試過從這裏讀取how to set check box state和其他帖子,但我只是沒有弄清楚我的代碼有什麼問題。

我的Java代碼:

public class OrderActivity extends Activity implements OnClickListener{ 
public static String orderOne = null; 
public static String orderTwo = null; 
public static String orderThree = null; 
public static String orderFour = null; 
public static String orderFive = null; 
public static String orderSix = null; 

CheckBox chkChickenBreast, chkChickenWings, chkChickenTighs, chkWholeChicken, chkSpicyWings, chkChickenFillet; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_order); 

    chkChickenBreast = (CheckBox) findViewById(R.id.check_box_order1); 
    chkChickenWings = (CheckBox) findViewById(R.id.check_box_order2); 
    chkChickenTighs = (CheckBox) findViewById(R.id.check_box_order3); 
    chkWholeChicken = (CheckBox) findViewById(R.id.check_box_order4); 
    chkSpicyWings = (CheckBox) findViewById(R.id.check_box_order5); 
    chkChickenFillet = (CheckBox) findViewById(R.id.check_box_order6); 

    Button checkOutOrder = (Button) findViewById(R.id.button_confirm_order); 
    checkOutOrder.setOnClickListener(this); 

    Button backToMain = (Button) findViewById(R.id.button_back_to_main); 
    backToMain.setOnClickListener(this); 


} 

@Override 
public void onClick (View view) { 
    String[] orderedProduct = new String[6]; 

    if (view.getId() == R.id.check_box_order1){ 
     if (chkChickenBreast.isChecked()){ 
      chkChickenBreast.setChecked(true); 
      orderedProduct[0] = "True"; 
     } 
     else{ 
      chkChickenBreast.setChecked(false); 
      orderedProduct[0] = "False"; 
     } 
    }    

    if (view.getId() == R.id.button_confirm_order){   
     Intent goToReceipt = new Intent(this, ReceiptActivity.class); 

     if (chkChickenWings.isChecked() == true) 
      orderedProduct[1] = "True";     
     else 
      orderedProduct[1] = "False"; 

     if (chkChickenTighs.isChecked() == true) 
      orderedProduct[2] = "True";     
     else 
      orderedProduct[2] = "False"; 

     if (chkWholeChicken.isChecked() == true) 
      orderedProduct[3] = "True";     
     else 
      orderedProduct[3] = "False"; 

     if (chkSpicyWings.isChecked() == true) 
      orderedProduct[4] = "True";     
     else 
      orderedProduct[4] = "False"; 

     if (chkChickenFillet.isChecked() == true) 
      orderedProduct[5] = "True";     
     else 
      orderedProduct[5] = "False"; 

     goToReceipt.putExtra(orderOne, orderedProduct[0]); 
     goToReceipt.putExtra(orderTwo, orderedProduct[1]); 
     goToReceipt.putExtra(orderThree, orderedProduct[2]); 
     goToReceipt.putExtra(orderFour, orderedProduct[3]); 
     goToReceipt.putExtra(orderFive, orderedProduct[4]); 
     goToReceipt.putExtra(orderSix, orderedProduct[5]); 

     startActivity(goToReceipt); 
    } 
    else if (view.getId() == R.id.button_back_to_main){ 
     onBackPressed(); 
     //Intent goToMenu = new Intent(this, MainActivity.class); 
     //startActivity(goToMenu); 
    } 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 

我的XML代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@layout/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="center" 
android:gravity="center_horizontal" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ImageView 
    android:id="@+id/image_robinsons_logo" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:contentDescription="@string/robinsons_picture_title" 
    android:src="@drawable/rsc" /> 

<ScrollView 
    android:id="@+id/scroll_order_product" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignLeft="@+id/image_robinsons_logo" 
    android:layout_below="@+id/image_robinsons_logo" > 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="0,1,2" > 

     <TableRow 
      android:id="@+id/table_row_title" 
      android:layout_width="fill_parent" > 

      <TextView 
       android:id="@+id/text_view_title_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:textSize="12sp" 
       android:text="@string/row_title_name" /> 

      <TextView 
       android:id="@+id/text_view_title_price" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:textSize="12sp" 
       android:text="@string/row_title_price" /> 

      <TextView 
       android:id="@+id/text_view_title_image" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:textSize="12sp" 
       android:text="@string/row_title_image" /> 

     </TableRow> 
    </TableLayout> 
</ScrollView> 

然後將其轉移到其他活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_receipt); 

    Intent goToReceipt = getIntent(); 
    String boolOrderOne = goToReceipt.getStringExtra(OrderActivity.orderOne); 

    if (boolOrderOne == "True"){ 
     TextView orderOne = new TextView(this); 
     orderOne.setText(boolOrderOne); 
     setContentView(orderOne); 
    } 
} 
+1

首先,您不能使用==符號比較字符串,您必須使用equals或equalsIgnoreCase,像這樣boolOrderOne .equalsIgnoreCase(「True」)。第二件事是你初始化orderOne,orderTwo ......他們看看所有的空值 –

+0

哪一行你不想包括?還要確保爲orderOne,orderTwo,orderThree分配唯一的字符串值,如orderOne =「OrderOne」,orderTwo =「orderTwo」等,這些被視爲訪問您的數據的關鍵。 –

+0

@ Moh.Sukhni我已經使用.equalsIgnoreCase完成了你的指令,但是我仍然在行TextView orderOne = new TextView(this); orderOne.setText(boolOrderOne); setContentView(orderOne);' – PaulPolon

回答

0

嘗試這種形式:

這剪斷上oncreat代碼:

check = (CheckBox) findViewById(R.id.checkBox); 
    // get state of checkbox 
    boolean isChecked = getBooleanFromPreferences("isChecked"); 
    // set state checkbox 
    check.setChecked(isChecked); 

    // invoked if status change 
    check.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton view, boolean isChecked) { 
      CheckInActivity.this.putBooleanInPreferences(isChecked, 
        "isChecked"); 
     } 
    }); 

    check.setOnClickListener(isCheckBoxClicked); 

方法:

public void putBooleanInPreferences(boolean isChecked, String key) { 

    SharedPreferences sp = this.getPreferences(Activity.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sp.edit(); 
    editor.putBoolean(key, isChecked); 
    editor.commit(); 

} 


public boolean getBooleanFromPreferences(String key) { 

    SharedPreferences sp = this.getPreferences(Activity.MODE_PRIVATE); 
    Boolean isChecked = sp.getBoolean(key, false); 
    return isChecked; 

} 

// if state of checkbox is true 
private OnClickListener isCheckBoxClicked = new OnClickListener() { 
    public void onClick(View v) { 

     if ((check.isChecked())) { 

      // Your code if checkbox state is true 

     } else { 


     } 
    } 

}; 

也許它會幫助你去嘗試其他的解決辦法。