无法在AlertDialog – Android中显示Spinner DropDown列表

我在AlertDialog中有两个微调器,微调器看起来很好,并且项目列表是正确的,它显示了每个列表的第一项.但是当我单击两个微调器中的任何一个时,下拉列表不会显示以选择其他一些项目.纺纱厂什么都不做.当我是AlertDialog之外的两个微调器时,这不会发生.

这是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) {

            }
        });
    }

和微调器的xml:

<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>

和图像:

任何人都知道任何可能的阴谋显示下拉列表?

最佳答案 我只是复制了你的代码并编辑了ArrayList.它完全适合我.

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) {

        }
    });
}
点赞