Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't show Spinner DropDown list inside AlertDialog - Android

I have two spinners in an AlertDialog, the spinners look good, and the list of items is correct, it shows the first items of each list. But when I click any of the two spinner, the dropdown list is not displayed to select some other item. The spinners do nothing. This does not happen when I was the same two spinners outside the AlertDialog.

This is the code of AlertDialog:

private void mostrar_alertdialog_spinners() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        TextView title = new TextView(this);
        title.setText("Selecciona un archivo:");
        title.setPadding(10, 10, 10, 10);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.rgb(0, 153, 204));
        title.setTextSize(23);
        builder.setCustomTitle(title);

        LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout_spinners = inflater.inflate(R.layout.layout_spinners,null);
        sp_titulos_carpetas = (Spinner) layout_spinners.findViewById(R.id.spinner_titulo_carpetas);
        sp_titulos_textos = (Spinner) layout_spinners.findViewById(R.id.spinner_textos_carpetas);

        builder.setView(layout_spinners);
        builder.setCancelable(false);
        builder.show();

        //configuracion de textos en memoria sd
        String path = Environment.getExternalStorageDirectory().toString()+"/Textos/";
        File f = new File(path);
        String[] fileStr = f.list();
        ArrayList<String> lista_lista_CARPETAS = new ArrayList<String>();
        for (String lista_texto : fileStr) {
            lista_lista_CARPETAS.add(lista_texto);
        }
        Collections.sort(lista_lista_CARPETAS, new AlphanumComparator());

        String[] lista_k = f.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                File f = new File(dir, name);
                return f.isDirectory();
            }
        });
        FileFilter fileFilter = new FileFilter() {
            public boolean accept(File file) {
                return file.isDirectory();
            }
        };
        File[] files = f.listFiles(fileFilter);

        ArrayAdapter<String> carpetas = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, lista_k);
        carpetas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
        sp_titulos_carpetas.setAdapter(carpetas);

        //ARRAY CON TITULOS DE ARCHIVOS TXT
        String camino = Environment.getExternalStorageDirectory().toString()+"/Textos/" + "Naxos"+ "/";
        File t = new File(camino);
        String[] lista_textos = t.list();
        ArrayList<String> lista_lista_textos = new ArrayList<String>();
        for (String lista_texto : lista_textos) {
            if (lista_texto.toLowerCase().endsWith(".txt")) {
                lista_lista_textos.add(lista_texto);
            }
        }
        for (int index =0; index < lista_lista_textos.size(); index++){
            lista_lista_textos.set(index, WordUtils.capitalizeFully(lista_textos[index].toLowerCase().replace(".txt", "")));
        }
        Collections.sort(lista_lista_textos, new AlphanumComparator());

        ArrayAdapter<String> adaptador_textos = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, lista_lista_textos);
        adaptador_textos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
        sp_titulos_textos.setAdapter(adaptador_textos);
        sp_titulos_textos.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String nombre_texto = parent.getSelectedItem().toString();
                File sdcard = new File( Environment.getExternalStorageDirectory().toString()+"/Textos/" + "Naxos/");

                //Get the text file
                File file = new File(sdcard, nombre_texto);

                //Read text from file
                StringBuilder text = new StringBuilder();

                int BUFFER_SIZE = 8192;

                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "Cp1252"),BUFFER_SIZE);
                    String line;

                    while ((line = br.readLine()) != null) {
                        text.append(line);
                        text.append('\n');
                    }
                }
                catch (IOException e) {
                    //You'll need to add proper error handling here
                }
                String nuevoTexto = text.toString().replaceAll("\t", " ");
                String nuevoTextoA = nuevoTexto.replaceAll("\n", " ");
                Holmes1 = nuevoTextoA;
                delimitadores = " ";
                tokenHolmes1 = new StringTokenizer(Holmes1, " ");
                arrayHolmes1 = Holmes1.split(delimitadores);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

And the xml for the spinners:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="100"
    style="@style/spinner_rojo">
    <Spinner
        android:id="@+id/spinner_titulo_carpetas"
        android:layout_width="0dp"
        style="@style/spinner_rojo"
        android:background="@drawable/spinner_background_holo_light"
        android:layout_height="wrap_content"
        android:layout_weight="50"></Spinner>

    <Spinner
        android:id="@+id/spinner_textos_carpetas"
        android:layout_width="0dp"
        style="@style/spinner_rojo"

        android:background="@drawable/spinner_background_holo_light"
        android:layout_height="wrap_content"
        android:layout_weight="50"></Spinner>

</LinearLayout>

And an image:

enter image description here

Anyone know any possible sulucion to show the drop down list?

like image 633
Oscar Méndez Avatar asked Nov 02 '22 10:11

Oscar Méndez


1 Answers

I just copied your code and edit ArrayList. It totally worked for me.

enter image description here

private void mostrar_alertdialog_spinners() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    TextView title = new TextView(this);
    title.setText("Selecciona un archivo:");
    title.setPadding(10, 10, 10, 10);
    title.setGravity(Gravity.CENTER);
    title.setTextColor(Color.rgb(0, 153, 204));
    title.setTextSize(23);
    builder.setCustomTitle(title);

    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout_spinners = inflater.inflate(R.layout.spinner_layout,null);
    Spinner sp_titulos_carpetas = (Spinner) layout_spinners.findViewById(R.id.spinner_titulo_carpetas);
    Spinner sp_titulos_textos = (Spinner) layout_spinners.findViewById(R.id.spinner_textos_carpetas);

    builder.setView(layout_spinners);
    builder.setCancelable(false);
    builder.show();


    ArrayList<String> lista_k = new ArrayList<String>();
    lista_k.add("Path A");
    lista_k.add("Path B");

    ArrayAdapter<String> carpetas = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, lista_k);
    carpetas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
    sp_titulos_carpetas.setAdapter(carpetas);



    ArrayList<String> lista_lista_textos = new ArrayList<String>();
    lista_lista_textos.add("Path C");
    lista_lista_textos.add("Path D");

    ArrayAdapter<String> adaptador_textos = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, lista_lista_textos);
    adaptador_textos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
    sp_titulos_textos.setAdapter(adaptador_textos);
    sp_titulos_textos.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}
like image 189
Thein Avatar answered Nov 13 '22 01:11

Thein