从Spinner获取选定的值

时间:2014-03-31 09:57:44

标签: android android-spinner

我制作了一个Slider,指的是this教程。我做了一些改变。我为每个图像添加了一个微调器和一个editText,以便用户可以输入内容并选择提供的选项。

我的活力是这样的:

public class Details extends Activity{

    ...
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        session = new SessionMngr(getApplicationContext());
        cart = new ShoppingCart(getApplicationContext());

        uid = session.getIntValues("uid");

        selectedItems = new ArrayList<String>();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);

        selectedItems = cart.getCartImages();

        viewPager = (ViewPager) findViewById(R.id.pager);

        Intent i = getIntent();
        int position = i.getIntExtra("position", 0);
        adapter = new FullScreenImageAdapter(Details.this, selectedItems);
        viewPager.setAdapter(adapter);

        viewPager.setCurrentItem(position); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.details_action_bar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        nvpSizes = new ArrayList<NameValuePair>();
        for(int i=0;i<selectedItems.size();i++){
            nvpSizes.add(new BasicNameValuePair("1", "1")); //qty , sizeId
        }

        switch(item.getItemId()){
            case R.id.action_check_out:
                dialog = ProgressDialog.show(this, "", "Please wait. Uploading file...",true);
                new Thread(new Runnable(){
                    public void run(){
                        UploadFile(selectedItems, nvpSizes);
                    }
                }).start();
                break;
            case R.id.action_add_more:

                // I want to get the Scanner value and editText value of all the scanners and editTexts here

                Intent intent = new Intent(Details.this, Dashboard.class);
                startActivity(intent);
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    ...
    ...
}

我的 FullScreenImageAdapter 是这样的:

public class FullScreenImageAdapter extends PagerAdapter {

    public FullScreenImageAdapter(Activity activity,
            ArrayList<String> imagePaths) {
        this._activity = activity;
        this.context = _activity.getApplicationContext();
        this._imagePaths = imagePaths;
    }

    ...
    ...

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imgDisplay;
        inflater = (LayoutInflater) _activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
                false);

        imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this._activity,
                android.R.layout.simple_spinner_item, sizes);

        Spinner mSpinner = (Spinner) viewLayout.findViewById(R.id.sizes);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
        imgDisplay.setImageBitmap(bitmap);

        ((ViewPager) container).addView(viewLayout);

        return viewLayout;
    }

    ...
    ...
}

我的活动在操作栏中有两个按钮,在按钮上单击我想要访问所有微调器和edittext的信息。我不知道得到它。我试过它:

Spinner mSpinner = (Spinner) viewLayout.findViewById(R.id.sizes);
mSpinner.getSelectedItem();

但上面的代码只返回一个值。 如何获得所有设置的微调器。

1 个答案:

答案 0 :(得分:1)

尝试覆盖以下功能:

mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            mSpinner.setSelection(i, true);
        }

然后你可以使用函数

mSpinner.getSelectedItem();

- 编辑 - 要获得选定的值,您可以获得所选的位置(整数),然后从它的适配器/值数组中获取值

mSpinner.getSelectedItemPosition();
祝你好运