首页 > 解决方案 > Android Spinner with different Value

问题描述

I created a spinner in my xml with the options permanent and temp and when the user clicks on it, it has to add to my azure database, the only problem i am facing is that if the user clicks permanent it must add a 1 to my database and if the user clicks temp it must add a 0 to my database

Here is my strings.xml

    <string-array name="customerstatus">

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

    </string-array>

标签: androidandroid-spinner

解决方案


您可以使用

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);

推荐阅读