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

3 回答 3

4

发生这种情况是因为您在循环中所做的只是在' 布局for中搜索 a ,然后使用 the 的方法添加它(此方法不会将视图添加到另一个已经存在的视图,它只是替换它们)。如果你想添加多个到同一个,你应该这样做:SpinnerActivitysetViewAlertDialogViewsSpinnerAlertDialog

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

编辑: 您可以使用适当的布局管理器添加更多视图。例如,对于同一行上的aTextView和 a ,您可以使用 a :SpinnerTableLayout

    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);
于 2012-05-05T09:33:03.757 回答
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);
于 2012-05-05T09:43:19.930 回答
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();
于 2012-05-05T09:39:36.460 回答