2

I need to display a dialogue with a list of colors to choose from. I found this solution here.

CharSequence colors[] = new CharSequence[] {"red", "green", "blue", "black"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // the user clicked on colors[which]
    }
});
builder.show();

I already have a String array of colors. How can I convert it to a CharSequence? I was thinking to use type casting

CharSequence colors[] = (CharSequence) mStringArray;

But this route does not work

Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163

1 Answers1

10

A String is already a CharSequence and since arrays are covariant in Java, a String[] is already a CharSequence[]. You probably don't need a cast at all, but if you use one it should be (CharSequence[]) mStringArray.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521