lundi 22 février 2016

Receive data from check boxes in different Activity

I have several checkboxes in my Activity_Main.XML similar to as follows

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Video"
        android:id="@+id/Video"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/VideoCheck"
            android:onClick="onCheckboxClicked"/>

Now on a different activity I want the state of this checkbox to be displayed, for ease of use I have set up the code so it will change the text or a text label. Code in my MenuActivity.java is as follows

public void onCheckboxClicked(View view) {
    boolean checked = ((CheckBox) view).isChecked();
    switch (view.getId())
    {
        case R.id.VideoCheck:
            if (checked) {
                TextView MyTextLabel = (TextView)findViewById(R.id.Test1);
                MyTextLabel.setText("Video");
            }
            else
            {
                TextView MyTextLabel = (TextView)findViewById(R.id.Test1);
                MyTextLabel.setText("No Video");
            }
            break;
    }
}

Worth saying the text label on the XML display is called MyTextLabel. I think the problem is because the Checkboxes are set up to call "onCheckboxClicked" but that checkbox clicked part is not in the Activity native to that set up.

Essentially the first page(activity_main.xml, MainActivity.Java) of my app has the checkboxes, then a button takes the user to the second page (activity_menu.xml, MenuActivity.java) has the Label set to change depending on the state of the checkboxes on the first page.

I appreciate this is explained horrendously but please ask any question you may have.




Aucun commentaire:

Enregistrer un commentaire