2012-05-05 62 views
1

我在循環中添加說'n'微調器。但是當我運行我的代碼時,只有最後添加的微調器纔可見。請參閱我下面的代碼在循環中動態添加微調器

Context context = this; 
     ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>(); 

     ArrayList<String> hi = new ArrayList<String>(); 
     hi.add("Test 1"); 
     hi.add("Test 2"); 

     list.add(hi); 

     ArrayList<String> hi1 = new ArrayList<String>(); 
     hi1.add("Test 3"); 
     hi1.add("Test 4"); 

     list.add(hi1); 

     AlertDialog.Builder alert = new AlertDialog.Builder(context); 

     alert.setTitle("Test App"); 
     alert.setMessage("Testing"); 


     for (int i = 0; i < 2; i++) { 
      final Spinner spinner = (Spinner) layout 
        .findViewById(R.id.spinner1); 
      ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_spinner_item, list.get(i)); 

      dataAdapter 
        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      spinner.setAdapter(dataAdapter); 
        alert.setView(spinner); 
     } 

     alert.show(); 

回答

4

正在發生,因爲所有你在for循環做的是尋找在Activity一個Spinner「佈局,然後使用AlertDialogsetView方法添加它(這個方法沒有按」將視圖添加到其他Views已經存在,它只是取代它們)。如果你想多Spinner添加到同一AlertDialog你應該做這樣的:

LinearLayout ll = new LinearLayout(this); 
ll.setOrientation(LinearLayout.VERTICAL); 
for (int i = 0; i < 2; i++) { 
    Spinner spinner = new Spinner(this); 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_spinner_item, list.get(i)); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(dataAdapter); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    ll.addView(spinner, params); 
} 
alert.setView(ll); 

編輯: 您可以通過使用適當的佈局管理器添加更多的意見。例如,對於在同一行TextViewSpinner你可以使用一個TableLayout

TableLayout tl = new TableLayout(this); 
    for (int i = 0; i < 2; i++) { 
     TableRow tr = new TableRow(this); 
     TextView t = new TextView(this); 
     t.setText("Text " + i); 
     TableRow.LayoutParams params1 = new TableRow.LayoutParams(
       TableRow.LayoutParams.WRAP_CONTENT, 
       TableRow.LayoutParams.WRAP_CONTENT); 
     tr.addView(t, params1); 
     Spinner spinner = new Spinner(this); 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, list.get(i)); 
     dataAdapter 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(dataAdapter); 
     TableRow.LayoutParams params2 = new TableRow.LayoutParams(
       TableRow.LayoutParams.FILL_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT); 
     tr.addView(spinner, params2); 
     TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
       TableLayout.LayoutParams.FILL_PARENT, 
       TableLayout.LayoutParams.WRAP_CONTENT); 
     tl.addView(tr, trParams); 
    } 
    alert.setView(tl); 
+0

感謝的東西。它的工作.... – Allwyn

+0

說我想將文本視圖添加到循環。那是我想在同一行顯示textview和微調。我怎樣才能做到這一點 ?現在它顯示的文本視圖和微調獨立的行。 – Allwyn

+0

我試過了,它的工作。 – Allwyn

0

你只是設置了同樣的觀點兩次。你需要創建一個視圖組(在你的情況下,你需要一個LinearLayout)並創建幾個新的spinners放入它。但是,如果你已經知道你要多少紡紗廠,你可以創建它裏面的所有的紡紗一個新的佈局,然後這樣做:

LinearLayout viewGroup = (LinearLayout) getInflater().inflate(R.layout.dialog_spinners, null); 

    for (int i = 0; i < viewGroup.getChildCount(); i++) { 
     final Spinner spinner = (Spinner) viewGroup.getChildAt(i); 

     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, list.get(i)); 

     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(dataAdapter); 
    } 
    alert.setView(viewGroup); 

    alert.show(); 
0

這條線......

final Spinner spinner = (Spinner) layout.findViewById(R.id.spinner1); 

是每次循環迭代時總是從佈局中獲得相同的微調。你可能想要動態地創建每一次迭代。

而此行...

alert.setView(spinner); 

總是設置警報的觀點,如先前加載單微調。

你應該嘗試這樣反而

LinearLayout layout = new LinearLayout(getApplicationContext()); 
layout.setOrientation(LinearLayout.HORIZONTAL); 

for (int i = 0; i < 2; i++) { 
    final Spinner spinner = new Spinner(getApplicationContext()); 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list.get(i)); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(dataAdapter); 
    layout.addView(spinner); 
} 

alert.setView(layout);