P
P
Pavel Rekun2014-10-23 11:47:26
Android
Pavel Rekun, 2014-10-23 11:47:26

How to use String in AlertDialog instead of String[]?

In general, there was a problem, I have an int array, which I convert to a String array using Arrays.toString(); for use in the AlertDialog list. But the dialog itself doesn't want to accept saying it's not an array of String, but just one String.
How to make it suitable for dialogue?
Actually code:

String a = Arrays.toString(stack_data);
String[] test2 = a.substring(1,a.length()-1).split(", ");

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                        builder.setTitle("История");
                        builder.setItems(Arrays.toString(test2), new DialogInterface.OnClickListener() {

                       public void onClick(DialogInterface dialog, int item) {

                        }
                        });

                        AlertDialog alert = builder.create();
                        alert.show();

stack_data is actually an array itself.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2014-10-23
@Menos

builder.setItems(test2,new DialogInterface.OnClickListener() { Like
this. Why are you then converting test2 back to a string is not clear.

A
anyd3v, 2014-10-23
@anyd3v

Where did you even find that it should convert to String[] ?

package java.util;

public class Arrays {
...
    public static java.lang.String toString(boolean[] array) { /* compiled code */ }

    public static java.lang.String toString(byte[] array) { /* compiled code */ }

    public static java.lang.String toString(char[] array) { /* compiled code */ }

    public static java.lang.String toString(double[] array) { /* compiled code */ }

    public static java.lang.String toString(float[] array) { /* compiled code */ }

    public static java.lang.String toString(int[] array) { /* compiled code */ }

    public static java.lang.String toString(long[] array) { /* compiled code */ }

    public static java.lang.String toString(short[] array) { /* compiled code */ }

    public static java.lang.String toString(java.lang.Object[] array) { /* compiled code */ }
...
}

Go through the list and convert yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question