Sunday, November 2, 2014

Android : Issues with checkboxes checking other check boxes on App



I am trying to make a checkbox in android studio/java that if checked will result in a subset of other checkboxes checkable and checked.


The developer site says: abstract void setChecked(boolean checked) Change the checked state of the view


But this hasn't worked for me.


Current code:


Java:



package crc.cybereye;

public class CreateNew extends Activity {
LinearLayout background;
Button btnBack;
CheckBox checkbox_structural_info;
CheckBox checkbox_years_of;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
background = (LinearLayout) findViewById(R.id.background);
setContentView(R.layout.activity_create_new);
btnBack = (Button) findViewById(R.id.btnBack);
checkbox_years_of = (CheckBox) findViewById(R.id.checkbox_years_of);
checkbox_floors_above = (CheckBox) findViewById(R.id.checkbox_floors_above);
checkbox_structural_info = (CheckBox) findViewById(R.id.checkbox_structural_info);
// setUpIntroCheckBoxes(); // Add change listeners to check boxes


btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), NewFormActivity.class);
startActivityForResult(intent, 0);
}
});
}


More Java:



public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_structural_info:
if (checked){
checkbox_years_of checked.setChecked(true) //ERROR HERE
}
break;
case R.id.checkbox_years_of:
if (checked){

}
break;


XML:



<TableRow android:layout_marginTop="10dp">
<TextView
android:text="@string/structural_info"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="@+id/checkbox_structural_info"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
</TableRow>
<TableRow>
<TextView
android:text="@string/years_of"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="@+id/checkbox_years_of"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
<TableRow>


There is an error saying that it cannot resolve method setChecked(boolean).


My code currently is just trying to get the structural_info checkbox to check the years_of checkbox if it is checked, but I also want to make years_or only checkable if structural_info is checked, I haven't got there yet because I was still stuck on this issue. Thanks so much for any help in advance.


No comments:

Post a Comment