-2

这段代码运行良好,但它只动态创建一个 微调以及我想在不同的微调器中加载不同的项目请告诉我如何做到这一点?

public class DisciplineActivity extends Activity
    {  ArrayList<String> selectDisciplineArrayList,disciplineInfoArrayList;
        @Override
         protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.discipline_view);
         for(int i=0;i<20;i++)
    {
     disciplineInfoArrayList.add("select location item:"+i);
    }
      // for(int i=0;i<5;i++)
    //{
         Spinner disciplineInfoSpinner = new Spinner(this);
         ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                 this, android.R.layout.simple_spinner_item,disciplineInfoArrayList);
         spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );

         disciplineInfoSpinner = (Spinner) findViewById(R.id.disciplineinfo_spinner);
         disciplineInfoSpinner.setAdapter(spinnerArrayAdapter);
    }//
    }
4

1 回答 1

0

您正在创建一个新的 Spinner 并且什么都不做。

您需要在“discipline_view”布局上创建一个空的 LinearLayout,然后在该 LinearLayout 上添加您创建的 Spinner,如下所示:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i=0;i<5;i++) {
     Spinner disciplineInfoSpinner = new Spinner(this);
     ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
             this, android.R.layout.simple_spinner_item,disciplineInfoArrayList);
     spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );

     disciplineInfoSpinner.setAdapter(spinnerArrayAdapter);
     linearLayout.addView(disciplineInfoSpinner);
}
于 2013-11-07T13:01:12.930 回答