如何在子视图中处理UI组件

时间:2014-01-02 23:47:51

标签: android android-layout spinner subview

在我的Android应用程序中,我有一个活动,顶部有一个微调器,下面有一个布局。所以,我的布局xml文件由微调器和LinearLayout组成。当我在微调器下拉列表中选择不同的选项时,我能够更改布局。在其中一个布局中,我有另一个微调器,并且,即使我设置其适配器以填充ArrayList的内容(我在调试中检查;它是非空的),选项不会显示在微调器中

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.new_task);

        Bundle bundle = getIntent().getBundleExtra("bundle");
        subjects = bundle.getStringArrayList("subjects");

        layout = (LinearLayout) findViewById(R.id.newtask_layoutbelow);
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout.addView(layoutInflater.inflate(R.layout.new_homework, null));

        taskspinner = (Spinner) findViewById(R.id.newtask_taskspinner);
        taskspinner.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, tasks));

        taskspinner
        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                int position = taskspinner.getSelectedItemPosition();
                layout.removeAllViews();
                LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                switch(position) {
                case 0:
                    layout.addView(layoutInflater.inflate(R.layout.new_homework, null));
                    break;
                case 1:
                    layout.addView(layoutInflater.inflate(R.layout.new_study, null));
                    break;
                case 2:
                    layout.addView(layoutInflater.inflate(R.layout.new_project, null));
                    break;
                case 3:
                    layout.addView(layoutInflater.inflate(R.layout.new_subject, null));
                    break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });



        name = (EditText)  layout.findViewById(R.id.new_homework_name);
        subjectspinner = (Spinner) layout.findViewById(R.id.new_homework_subjectspinner);
        date = (Button) layout.findViewById(R.id.new_homework_selectdate);
        Log.e("arr",subjects.toString());
        subjectspinner.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, subjects));

        create = (Button) layout.findViewById(R.id.new_homework_create_button);

        subjectspinner
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        int position = subjectspinner.getSelectedItemPosition();
                        subject = subjects.get(position);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
                });
    }

0 个答案:

没有答案