0

I need to create checkboxes dynamically in android. I am getting the value in my code but unable to set that value to checkbox.

My Code:

CheckBox[] cbs = new CheckBox[20]; 
for(int k=0; k<stringList3.size(); k++)
{
 System.out.println("stringlist3 in for loop"+stringList3.get(0));
 arr = stringList3.get(k);
 cbs[k] = new CheckBox(getContext());
 System.out.println("arr values"+arr.get(0));
 System.out.println("arr values"+arr.get(1));
 System.out.println("arr values"+arr.get(2));
 cbs[k].setText((CharSequence) arr.get(2));
 Rl.addView(cbs[k]);
                 } 

Here when I am setting the value arr.get(2) to checkbox it is not setting ...please help me regarding this... Thanks in advance

7
  • Try with constant string like "Check Box Testing" in place of (CharSequence) arr.get(2) confirm whether the problem with arr or cbs[k] try it Commented Jan 31, 2012 at 4:58
  • hi i have tried keeping like cbs[k].setText("hello");..but even then text is not setting...do we need to create a textview in layout for dynamically generated checkboxes? Commented Jan 31, 2012 at 5:08
  • is Rl relative layout in XML file? Commented Jan 31, 2012 at 5:27
  • yes Rl layout i am referring from my xml file...checkboxes are displaying but only that text is not setting to it..please help me Commented Jan 31, 2012 at 5:30
  • can you post the declarations and definitions of stringList3 and arr ? Commented Jan 31, 2012 at 5:40

1 Answer 1

1

I don't know how you written remaining code, just check your code with below example.

Dynamically adding views to layout

I think in your code, the problem may be stringList3.size() returning more than 20, so that you are getting force close. Just check it once.

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

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.