Android 具有不同值的微调器

Android Spinner with different Value

我在我的 xml 中创建了一个带有选项 permanenttemp 的微调器,当用户点击它时,它必须添加到我的 azure 数据库中,这是唯一的问题我面临的是,如果用户单击 permanent,它必须向我的数据库添加一个 1,如果用户单击 temp,它必须向我的数据库添加一个 0

这是我的 strings.xml

    <string-array name="customerstatus">

        <item>permanent</item>
        <item>temporary</item>

    </string-array>

您可以使用

spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
        {
            String selectedItem = parent.getItemAtPosition(position).toString();
            if(selectedItem.equalsIgnoreCase("permanent")) {
                 // Value should be 1 
            } else if(selectedItem.equalsIgnoreCase("temp")) {
                 // Value should be 0 
            }
        } // to close the onItemSelected
        public void onNothingSelected(AdapterView<?> parent) 
        {

        }           
    });

您还可以按照此处所述通过代码在微调器中添加动态值

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_spinner_item, new String [] {"USA", "Europe"});

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