如何获取 Spinner 值以保存在变量中

How to get the Spinner value to save in a variable

我知道围绕这个有几个问题,但不太明白如何解决。 问题是我正在显示本地 SQlite 数据库中的一些值,不同的选项显示正常,我可以 select 它们并且显示的值正常。问题是当我尝试保存它时,getSelectedItem 获取列表中的第一项。任何关于如何解决它的帮助或建议都会很棒。

        Product product = new Product();
        productsList = product.getProducts();

        Spinner spinnerProduct = findViewById(R.id.spinnerProduct);

        String[] arrayProduct = new String[productsList.size()];
        for(int i = 0; i < productsList.size(); i++) {
            arrayProduct[i] = productsList.get(i).nameProduct;
        }

        ArrayAdapter<String> adapterProduct = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, arrayProduct);
        adapterProduct.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerProduct.setAdapter(adapterProduct);
        spinnerProduct.setOnItemSelectedListener(onItemSelectedListener1);

        String productSelected=spinnerProduct.getSelectedItem().toString();

AdapterView.OnItemSelectedListener onItemSelectedListener1 =
        new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                Product product = new Product();
                productsList = product.getProducts();
                int[] arrayProduct = new int[productsList.size()];
                for(int i = 0; i < productsList.size(); i++) {
                    arrayProduct[i] = productsList.get(i).stockCurrent;
                }
                String productStock = String.valueOf(arrayProduct[position]);
                product_amount_available.setText(productStock);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        };

您需要实施 OnItemSelectedListener 并覆盖

public void onItemSelected(AdapterView<?> parent, View view,
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

可以在此处找到更多信息https://developer.android.com/guide/topics/ui/controls/spinner#SelectListener

微调器使用事件侦听,因此您不能仅以线性方式执行以下操作:

spinnerProduct.setOnItemSelectedListener(onItemSelectedListener1);

String productSelected=spinnerProduct.getSelectedItem().toString();

基本上您的代码所做的是设置侦听器,紧接着,它从您的 spinnerProduct 对象获取一些 arbitrary/default 值。但是您甚至还没有向微调器输入任何内容。您必须仅在事件侦听器的 onItemSelected() 方法中处理所有 UI 和业务逻辑。

感谢您的帮助。我解决了。我做了全局变量。

int idProduct,idStorage;
String productSelected,storageSelected;

首先填充旋转器

Product product = new Product();
        productsList = product.getProducts();
        Storage storage = new Storage();
        storageList = storage.getStorage();

        Spinner spinnerProduct = findViewById(R.id.spinnerProduct);
        spinnerProduct.setOnItemSelectedListener(this);

        Spinner spinnerStorage = findViewById(R.id.spinnerStorage);
        spinnerStorage.setOnItemSelectedListener(this);

        String[] arrayProduct = new String[productsList.size()];
        for(int i = 0; i < productsList.size(); i++) {
            arrayProduct[i] = productsList.get(i).nameProduct;
        }
        ArrayAdapter<String> adapterProduct = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, arrayProduct);
        adapterProduct.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerProduct.setAdapter(adapterProduct);
        spinnerProduct.setOnItemSelectedListener(this);

        String[] arrayStorage = new String[storageList.size()];
        for(int i = 0; i < storageList.size(); i++) {
            arrayStorage[i] = storageList.get(i).nameStorage;
        }
        ArrayAdapter<String> adapterStorage = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,  arrayStorage);
        adapterStorage.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerStorage.setAdapter(adapterStorage);
        spinnerProduct.setOnItemSelectedListener(this);

然后,按照建议实施onclicklisteners

@Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {

        if(adapterView.getId() == R.id.spinnerProduct)
        {
            idProduct=(int) adapterView.getSelectedItemId();
            productSelected=adapterView.getSelectedItem().toString();

            Product product = new Product();
            productsList = product.getProducts();
            int[] arrayProduct = new int[productsList.size()];
            for(int i = 0; i < productsList.size(); i++) {
                arrayProduct[i] = productsList.get(i).stockCurrent;
            }
            String productStock = String.valueOf(arrayProduct[pos]);
            product_amount_available.setText(productStock);

        }
        else if(adapterView.getId() == R.id.spinnerStorage)
        {
            storageSelected=adapterView.getSelectedItem().toString();
            idStorage=(int) adapterView.getSelectedItemId();
        }

    }


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

    }

最后我将值传递给了由按钮触发的方法

btnSaveTransferProduct.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                        saveTransferProduct(idProduct,productSelected, idStorage, storageSelected);
                }
            }
        });