1

Im doing an App on Android Studio and im trying to add all my checkbox IDs into an array, so i can use it , without doing it manually. I tried to do it on another way, but i didn´t find nothing on google that help me,

So here is my goal:

I want to get all my Checkbox ids, so i can get their text. And i don´t want to do it manually because i got alot of checkboxs.

I tried to write a code by myself but i´m getting an message error. Here is my code:

 CheckBox[] MinhaCheckBox;
    SharedPreferences Dados;
   String MinhaPasta = "Pasta";
    String valor;

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

    public void btn_Gerar(View v)
    {
        Dados = getSharedPreferences(MinhaPasta,0);
        SharedPreferences.Editor Edita = Dados.edit();
        int IDTexBoz[] = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,
                R.id.checkBox6,R.id.checkBox7,R.id.checkBox8};
        for(int i = 0 ; i < IDTexBoz.length; i++)
        {


 *//I GOT AN ERRO HERE. please help me.*
 * // when i try to put mycheckbox on my array, i got th error!*
            MinhaCheckBox[i] = (CheckBox)findViewById(IDTexBoz[i]);

        }
        for (int a= 0; a < MinhaCheckBox.length;a++)
        {
            if(MinhaCheckBox[a].isChecked())
            valor += MinhaCheckBox[a].getText().toString() + ";";
        }
        Edita.putString("Dado", valor);
        Edita.commit();

        Intent MeuIntent = new Intent(this,Main2Activity.class);
        startActivity(MeuIntent);
    }
}
5
  • You haven't initialized MinhaCheckBox. Have you? Commented Oct 26, 2015 at 18:37
  • It will be easier to find solution, if you provide the exact error message. Commented Oct 26, 2015 at 18:39
  • I did, check out the first line please. Commented Oct 26, 2015 at 18:40
  • It's just declaration. What about initialization? Commented Oct 26, 2015 at 18:41
  • ohhh thank you, i didn´t notice. Commented Oct 26, 2015 at 18:48

3 Answers 3

1

You need to initialize your checkbox array.

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

1 Comment

It would be something like... CheckBox[] MinhaCheckBox = new CheckBox[IDTexBoz.length]; Put the line before the for statement.
1

Maybe this example could help, you don't need to store the ID name, do something like this..

for(int i=1;i<=12 ;i++){ 
        int resID = getResources().getIdentifier("checkBox"+i, "id",getPackageName()); 
        CheckBox cb = (CheckBox) findViewById(resID);
        //Handle cb object here  
}

Comments

0

It's Very Simple.

int IDTexBoz[] = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,
                R.id.checkBox6,R.id.checkBox7,R.id.checkBox8};
//Here is code for array initialization. 
MinhaCheckBox = new CheckBox[IDTexBoz.length];

Comments

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.