获得选择下拉项目操作栏Android?

时间:2014-09-19 18:31:04

标签: android android-actionbar

我正在学习动作栏。我像这样创建了AB

enter image description here

这个代码
被修改

@Override
    protected void onCreate(aBundle savedInstanceState) {
        ...
        SpinnerMenuForm = new ArrayList<SpinnerNavItem>();
        SpinnerMenuForm.add(new SpinnerNavItem("Fill Order - HSD Bunker", "02"));
        SpinnerMenuForm.add(new SpinnerNavItem("Fill Order - MFO Bunker", "14"));   
        adapterSpinnerMenuForm = new Adapter_List_Form(getApplicationContext(), SpinnerMenuForm);
        ...
    }

     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate our menu from the resources by using the menu inflater.
            getMenuInflater().inflate(R.menu.main, menu);
            MenuItem actionbar_datetime = menu.findItem(R.id.actionbar_datetime);
            MenuItem actionbar_username = menu.findItem(R.id.actionbar_username);
            actionbar_datetime.setTitle(currentDateandTime);
            actionbar_username.setTitle(Variabel.username);            
            MenuItem actionbar_form = menu.findItem( R.id.actionbar_form);
            View view1 = actionbar_form.getActionView();
            if (view1 instanceof Spinner)
            {
                final Spinner spinner = (Spinner) view1;
                spinner.setAdapter(adapterSpinnerMenuForm);    
                spinner.setOnItemSelectedListener(new OnItemSelectedListener() {                    
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view,
                            int position, long id) {
                        // TODO Auto-generated method stub
                        String text = spinner.getSelectedItem().toString();
                        Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();                      
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });   
            }
            return true;
        }

我想通过“String text = spinner.getSelectedItem().toString();选择下拉项目(绿色),但它不是正确的值”。这是怎么回事?

0 个答案:

没有答案