在循环中动态添加微调器

时间:2012-05-05 09:24:12

标签: android android-spinner

我在循环中添加说'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();

3 个答案:

答案 0 :(得分:4)

这种情况正在发生,因为您在for循环中所做的只是在Spinner'布局中搜索Activity,然后使用setView AlertDialog方法1}}添加它(此方法不会将视图添加到已存在的其他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);

答案 1 :(得分:0)

您只需将相同的视图设置两次。您需要创建一个视图组(在您的情况下,您需要LinearLayout)并创建几个新的微调器以放入其中。但是如果你已经知道你想要多少个微调器,你可以创建一个包含所有微调器的新布局,然后这样做:

    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();

答案 2 :(得分: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);