spinner adding string array on item selection how can get item related value in android

前端 未结 1 1150
孤独总比滥情好
孤独总比滥情好 2021-02-08 02:37

i am developing one spinner this spinner i am string array

spinner = (Spinner)this.findViewById(R.id.mlg); 
final CharSequence[] itemArray =getResources().getTe         


        
1条回答
  •  天涯浪人
    2021-02-08 03:22

    I am not sure either Spinner allow that attribute value in XML String or not but your problem can be solved like this.

    Create two arrays in your array.xml file like:

    
    
        
            Nokia 1200
            Nokia 1600
            Nokia 5130
        
        
            1000
            2000
            5000
        
    
    

    Now load first array in your adapter and store the second one in other Array to hold values of items as:

    String items [] = getResources().getStringArray(R.array.items);
    String values [] =  getResources().getStringArray(R.array.values);
    

    And you can simply get the respective item name and value in your onItemSelected() method like this:

    String item = parent.getItemAtPosition(pos).toString();
    String value = values [pos];
    

    0 讨论(0)
提交回复
热议问题