2013-11-27 26 views
4

我有一個AlertDialog中的兩個微調,spinners看起來不錯,並且項目列表是正確的,它顯示每個列表的第一項。但是,當我點擊兩個微調框中的任何一個時,下拉列表不會顯示爲選擇其他項目。紡紗工什麼都不做。當我是AlertDialog之外的兩個相同的旋轉器時,不會發生這種情況。無法顯示AlertDialog中的微調DropDown列表 - Android

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

和圖像:

enter image description here

任何人都知道任何可能sulucion顯示下拉列表?

+0

據我所知,你的問題是微調不能表現出它的下拉菜單選擇到選擇其它選項,對不對?我剛剛複製了你的代碼,它對我很好。你這人怎麼回事 ? – Megamind

回答

0

移動這對代碼的結束,所以你設置的一切行動後這樣做:

builder.show(); 
0

爲同一創建自定義警告對話框。試試這個

Dialog new_dialog = new Dialog(getParent());                
// new_dialog.setTitle("Book your appointment"); 
new_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
new_dialog.setContentView(R.layout.customize_dialog_list_view); 
new_dialog.setCancelable(false); 
cuc = new CommanUtilityClass(); 
SharedPreferences sp = getSharedPreferences("provider",0); 
String services = sp.getString("services", ""); 
TextView service = (TextView) new_dialog 
     .findViewById(R.id.cdlv_service_provider); 
TextView hour = (TextView) new_dialog.findViewById(R.id.cdlv_working_hours); 
TextView appointment_time = (TextView) new_dialog.findViewById(R.id.cdlv_appoint_time); 
TextView appointment_date = (TextView) new_dialog.findViewById(R.id.cdlv_appoint_date); 
//String[] ampm = myTiming[which].split(":"); 
/*String[] range = myTiming[which].split(":"); 
int startTimeInt = Integer.parseInt(range[0]) 
     * 60 + Integer.parseInt(range[1]); 
String finalvalue = ""; 
if(startTimeInt >= 720){ 
    if(startTimeInt >= 780){ 

    }else{ 

    } 
}else{ 
    finalvalue = String.valueOf(range[0] + ":" + range[1] + " AM"); 
} 

for (int i = 0; i < range.length; i++) { 
    String startTimeString = range[i].split("-")[0]; 
    String endTimeString = range[i].split("-")[1]; 
    Log.d("Minutes", "startTimeString = " + startTimeString); 
    Log.d("Minutes", "endTimeString = " + endTimeString); 
    int startTimeInt = Integer.parseInt(startTimeString.split(":")[0]) 
      * 60 + Integer.parseInt(startTimeString.split(":")[1]); 
    int endTimeInt = Integer.parseInt(endTimeString.split(":")[0]) * 60 
      + Integer.parseInt(endTimeString.split(":")[1]); 


}*/ 


appointment_time.setText(Html.fromHtml("<b>Appointment time :</b>" + myTimingToShow[which].split("/")[0])); 
appointment_date.setText(Html.fromHtml("<b>Appointment date :</b>" + selected)); 

service.setText(Html 
     .fromHtml("<b>Service provider :</b>" 
       + cuc.toTheUpperCase(bsp_name))); 

hour.setText(Html 
     .fromHtml("<b>Working hours :</b>" 
       + cuc.toTheUpperCase(bsp_availability))); 

try { 
    lv = (ListView) new_dialog 
      .findViewById(R.id.cdlv_list); 

    CustomDialogArrayAdapter cdaa = new CustomDialogArrayAdapter(
      getApplicationContext(), 
      m_ArrayList); 

    lv.setAdapter(cdaa); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
    new_dialog.show(); 

在這裏,我剛膨脹XML佈局提醒對話框。確保你將每個微調器的上下文都提取到對話框中。請參閱上面的代碼。

希望它有幫助。乾杯!

0

由於內存泄露,發生這樣,當你打開一個微調是能夠得到有效的範圍內,但是當你試圖獲取另一微調它實際上得到null的背景下第二次沒有填充任何東西。但是,當你在Alert-Dialog中使用Activity中的微調器時,其實際總是獲得valid context。因此,那個時候你沒有收到任何錯誤,並且它正確填充。

所以,爲了避免內存泄漏,使用getApplicationContext()檢索微調的背景下ArrayAdapter

ArrayAdapter<String> carpetas = new ArrayAdapter<String> 
     (getApplicationContext(),android.R.layout.simple_spinner_item, lista_k); 


ArrayAdapter<String> adaptador_textos = new ArrayAdapter<String> 
     (getApplicationContext(),android.R.layout.simple_spinner_item, lista_lista_textos); 
2

我只是複製你的代碼和編輯的ArrayList。它完全爲我工作。

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

     } 
    }); 
}