0

I want to store selected checkbox values in ArrayList. There is five checkbox if I select three then they will store on ArrayList. I used String []ad = new String[5]; is it write on not to store the value of checkbox

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
List<String> mList = new ArrayList<>();
CheckBox android, java, python, php, unity3D;
Button submitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    android = (CheckBox) findViewById(R.id.androidCheckBox);
    android.setOnClickListener(this);
    java = (CheckBox) findViewById(R.id.javaCheckBox);
    java.setOnClickListener(this);
    python = (CheckBox) findViewById(R.id.pythonCheckBox);
    python.setOnClickListener(this);
    php = (CheckBox) findViewById(R.id.phpCheckBox);
    php.setOnClickListener(this);
    unity3D = (CheckBox) findViewById(R.id.unityCheckBox);
    unity3D.setOnClickListener(this);

    submitButton = (Button) findViewById(R.id.submitButton);
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("ArrayList Values*******",mList.toString());
        }
    });

}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.androidCheckBox:
            if (android.isChecked()) {
                mList.add(String.valueOf(android.getText()));
                Log.e("Android*******",mList.toString());
            }
            break;

        case R.id.javaCheckBox:
            if (java.isChecked()) {
                mList.add(String.valueOf(java.getText()));
                Log.e("Java*******",mList.toString());
            }
            break;

        case R.id.phpCheckBox:
            if (php.isChecked()) {
                mList.add(String.valueOf(php.getText()));
                Log.e("PHP*******",mList.toString());
            }
            break;

        case R.id.pythonCheckBox:
            if (python.isChecked()){
                mList.add(String.valueOf(python.getText()));
                Log.e("Python*******",mList.toString());
            }
            break;

        case R.id.unityCheckBox:
            if (unity3D.isChecked()){
                mList.add(String.valueOf(unity3D.getText()));
                Log.e("Unity*******",mList.toString());
            }
            break;
      }
    }
  }

2 Answers 2

1

Just create a List and add values when your click events are fired:

final List<String> mList = new ArrayList<>();
mList.add("Your value");

Note: consider to implement onCheckChangeListener intead of onClickListener to handle your checkbox selection events.

Sign up to request clarification or add additional context in comments.

2 Comments

we can use ArrayList but I am new on android so can you tell me that how to add ArrayList.
I have done this please check my edited code. Now I need when someone do uncheck, the value of checkbox will be removed from ArrayList
0

No, it's not quite correct.

I strongly recommend creating the array when the user presses submitButton. Otherwise, if they check some boxes, and either

  1. Rotate the screen, or
  2. Put the app in the background and the Activity gets destroyed by the system (You can simulate this by selecting the "Don't keep Activities" option in Developer Options)

When they see your UI again, it will be correctly re-created - all the boxes the user has checked will still be checked, but your array will be empty! I recommend something like

submitButton = (Button) findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String []ad = new String[5];
        if (android.isChecked()) {
            ad[0] = (String) android.getText();                
        }
        if (java.isChecked()) {
            ad[1] = (String) java.getText();                
        }
        ...
    }
});

If you care about ad outside the context of submitting the user's choices, the best practice is to save it in the Bundle in public void onSaveInstanceState(Bundle outState) {} and fetch and set it in your onCreate(Bundle savedInstanceState){}. This way you will not loose data even on orientation change, or on the system destroying your Activity. See this answer for details on how to do that.

13 Comments

Whenever a user clicks the button your array will be reset, so you will lose previous stored data...
It will not, as the state is stored in the UI itself. Whenever the user clicks sumbitButton the code checks the state of every checkbox and creates the array based on the current state. I am suggesting you no longer write anything to the array in your switch statement, but only in the submitButton click listener.
Doing this: String []ad = new String[5]; within onClick callback, you create a new array instance...
Depends on what the asker wants to do with the array - the question did not specify this. I assumed the array has no utility beyond the submission code. Otherwise, it will need to be created outside the click listener, and persisted using Activity.onSaveInstanceState - however, the question does not mention if this is necessary.
Your code doesn't handle rotations or lifecycle events.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.