mardi 29 août 2017

how to store selected checkbox values in ArrayList

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 {

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) {
        }
    });

}

@Override
public void onClick(View view) {

I create one Array String for storing selected values.

    String []ad = new String[5];

Switch Case condition for onClick in checkbox

    switch (view.getId()) {
        case R.id.androidCheckBox:
            if (android.isChecked()) {
                ad[0] = (String) android.getText();
                Toast.makeText(getApplicationContext(), ad[0], Toast.LENGTH_LONG).show();
                Log.e("Android*********",ad[0]);
            }
            break;

        case R.id.javaCheckBox:
            if (java.isChecked()) {
                ad[1] = (String) java.getText();
                Toast.makeText(getApplicationContext(), ad[1], Toast.LENGTH_LONG).show();
                Log.e("Java*********", ad[1]);
            }
            break;

        case R.id.phpCheckBox:
            if (php.isChecked()) {
                ad[2] = (String) php.getText();
                Toast.makeText(getApplicationContext(), ad[2], Toast.LENGTH_LONG).show();
                Log.e("PHP*********", ad[2]);
            }
            break;

        case R.id.pythonCheckBox:
            if (python.isChecked()){
                ad[3] = (String) python.getText();
                Toast.makeText(getApplicationContext(), ad[3], Toast.LENGTH_LONG).show();
                Log.e("Java*********",ad[3]);
            }
            break;

        case R.id.unityCheckBox:
            if (unity3D.isChecked()){
                ad[4] = (String) unity3D.getText();
                Toast.makeText(getApplicationContext(), ad[4], Toast.LENGTH_LONG).show();
                Log.e("Java*********",ad[4]);
            }
            break;
    }
}




Aucun commentaire:

Enregistrer un commentaire