0

Hello I'm a student and My project is making a application by android studio.

I'm just a beginner so everything is difficult :(

Here is my problem.

I dynamically created checkbox depending on the current.

I want to get finalMemberTag if user click the btnEx button.

But In this code, When I click the btnEx, finalMemberTag is null.

And I want that If user click the checkbox, add the checkbox's text to the finalMemberTag. and checkbox is unchecked, remove the checkbox's text to the finalMemberTag.

How can I do these? (I already set the drawable things.)

Please Help me...


 private String[] finalMemberTag;

  Button btnEx;

  btnEx.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendData();
        }
    });


Switch(current){

  case "A":
                    String[] memberA = {"all", "ab", "cd", "ef", "g", "hu", "hi", "dd"};
                    
                    for (int i = 0; i < 8; i++) {
                        CheckBox chA = new CheckBox(getApplicationContext());
                        chA.setText(membersA[i]);
                        chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
                        chA.setTextColor(Color.parseColor("#8D8D8D"));
                        cHA.setButtonDrawable(android.R.color.transparent);
                        chA.setBackground(inactiveCb);
                        int index = i;
                        chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
                                    chA.setBackground(activeCb);
                                    chA.setTextColor(startextColor);
                                    finalMemberTag[index] = buttonView.getText().toString();
                                    
                                }

                                if(!isChecked) {
                                    chA.setBackground(inactiveCb);
                                    chA.setTextColor(Color.parseColor("#8D8D8D")); }
                                    
                            }
                        });


                        chA.setPadding(36, 24, 36, 24);
                        chA.setLayoutParams(params);
                        placeInputMember.addView(chA);
                    }
                    break;

        case "B":
                    String[] memberB = {"all", "abc", "Dcd", "e3f", "DSg", "DGu", "hE", "dV"};
                    
                    for (int i = 0; i < 8; i++) {
                        CheckBox chB = new CheckBox(getApplicationContext());
                        chB.setText(memberB[i]);
                        chB.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
                        chB.setTextColor(Color.parseColor("#8D8D8D"));
                        chB.setButtonDrawable(android.R.color.transparent);
                        chB.setBackground(inactiveCb);
                        int index = i;
                        chB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
                                    chB.setBackground(activeCb);
                                    chB.setTextColor(startextColor);
                                    finalMemberTag[index] = buttonView.getText().toString();
                                   
                                }

                                if(!isChecked) {
                                    chB.setBackground(inactiveCb);
                                    chB.setTextColor(Color.parseColor("#8D8D8D")); }
                                    
                            }
                        });


                        chB.setPadding(36, 24, 36, 24);
                        chB.setLayoutParams(params);
                        placeInputMember.addView(chB);
                    }
                    break;

  }    

    public void sendData(){
 Log.d("##", String.valueOf(finalMemberTag));

   }

ANOTHER ERROR

String[] membersA = {"all", "ji", "ha", "bo", "re", "co", "zo", "pi"};
                    arrayMember = new ArrayList<>();

for (int i = 0; i < membersBts.length; i++) {
                        CheckBox chA = new CheckBox(getApplicationContext());
                        chA .setText(members[i]);
                        chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
                        chA.setTextColor(Color.parseColor("#8D8D8D"));
                        chA .setButtonDrawable(android.R.color.transparent);
                        chA.setBackground(inactiveCb);
                        int index = i;
                        chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                if (isChecked) {
                                    activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
                                    chA.setBackground(activeCb);
                                    chA.setTextColor(startextColor);
                                    arrayMember.add(index, buttonView.getText().toString());
                                }

                                if(!isChecked) {
                                    chA.setBackground(inactiveCb);
                                    chA.setTextColor(Color.parseColor("#8D8D8D"));
                                    arrayMember.remove(index);
                                }

                            }
                        });

                        chA.setPadding(36, 24, 36, 24);
                        chA.setLayoutParams(params);
                        placeInputMember.addView(chA);
                    }
                    break;

ERROR LOGCAT

    java.lang.IndexOutOfBoundsException: Index: 5, Size: 2

1 Answer 1

1

Ok, you have some issues with your code:

First, finalMemberTag is always null because you never initialized it. If you want to use a string array and you want to store the text on the checkboxes then we should assume that at most you'll have all of the checkboxes (16) selected. This means you'll want to initialize your array as:

private String[] finalMemberTag = new String[16]

Second, I think you'd be better off using a string set to avoid duplicates, or, if you need the information on a particular index, an arraylist.

With the Set:

 private Set<String> finalMembersTag  =  new HashSet<>(16);
 //onCreate...other stuff...for loop
 //...setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         final String buttonText = buttonView.getText().toString();
         if (isChecked) {
             // other stuff ...
             finalMembersTag.add(buttonText);
         } else {
             // other stuff ...
             finalMembersTag.remove(buttonText);
         }
     }

With the ArrayList:

 private List<String> finalMembersTag  =  new ArrayList<>(16);
 //onCreate...other stuff...for loop
  int index = i;
  chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         final String buttonText = buttonView.getText().toString();
         if (isChecked) {
             // other stuff ...
             finalMembersTag.add(buttonText); //this will add the text
         } else {
             // other stuff ...
             finalMembersTag.remove(buttonText); //this will remove the element if found, or if you want an empty string you can do finalMembersTag.add(index, "");
         }
     }
Sign up to request clarification or add additional context in comments.

2 Comments

Hello I really thankful to your kindly answer. I tried it and it worked well for a while.(Click the checkbox a few) When I click the checkbox several time, I get a error like java.lang.IndexOutOfBoundsException Can you tell me How can I solve this? I search about it, but I don't know why my index is larger than size. I updated my Answer.
Yes, you have to be careful with the remove using an index, the array will be shorter, I changed the answer so you use finalMembersTag.remove(buttonText). Also, adding something at an index not existent will give you the same error, so best use finalMembersTag.add(buttonText) without the index in your particular case

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.