1

I guess this is a pretty newbie question, but I've spent only 2 weeks on Android.

My question is, I have a StringArray created with a reference R.array.NAME

I want to populate a Dialog full of checkboxes with the values from the StringArray, but it seems I need to convert it to CharSequence[] so I can use: setMultiChoiceItems

I can't find a way to do it.

This is my code (I have to add accept/cancel buttons anyway)

// I want this (subjects variable) to be gotten from my StringArray R.array.NAME

final CharSequence[] subjects = {"Sports", "History", "Maths"};         
final boolean[] states = {false, true, false};    
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMultiChoiceItems(subjects, states, new DialogInterface.OnMultiChoiceClickListener(){
            public void onClick(DialogInterface dialogInterface, int item, boolean state) {
            }
        });

Thanks a lot guys.

Tushar Gogna
  • 4,963
  • 6
  • 31
  • 42
Sento
  • 171
  • 5
  • 13

2 Answers2

8

From within your Activity call:

String[] subjects = getResources().getStringArray(R.array.NAME);

To obtain the value of the resource.

Hope that helps.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • But I need a CharSequence. I tried that and it doesn't work with the parameter of builder.setMultiChoiceItems – Sento Oct 15 '11 at 16:15
  • 1
    A String[] is a CharSequence[] and yes, it can be passed to AlertDialog.Builder – devunwired Oct 15 '11 at 16:25
  • 1
    @Sento you'll have to tell us what "doesn't work" means. A String[] can be used anywhere a CharSequence[] can. – panda Oct 15 '11 at 16:29
  • Ok man, I'm very sorry. I was still using getIntArray instead of getStringArray. Now it's all working. I apologize :_ – Sento Oct 15 '11 at 16:30
0
CharSequence[] subjects =getResources().getStringArray(R.array.subjectsName);
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109