0

I have a spinner with some string items. how can i assign an id to each of this items and get the id when the item is clicked.

<string-array name="country_arrays">
    <item id=10>Malaysia</item>
    <item id=23>United States</item>
    <item id=22>Indonesia</item>
    <item id=6>France</item>
    <item id=5>Italy</item>
    <item id=4>Singapore</item>
    <item id=3>New Zealand</item>
    <item id=2>India</item>
</string-array>

This is my String array. each the item in my string has an array has an id. now how can i get the id when the item is selected. is there any other way to assign ID to the string and get it back when the item is pressed in the spinner?

null pointer
  • 5,874
  • 4
  • 36
  • 66
  • possible duplicate of [How can I add items to a spinner in Android?](http://stackoverflow.com/questions/5241660/how-can-i-add-items-to-a-spinner-in-android) – Alper Turan May 01 '15 at 08:17

1 Answers1

2

You can get the position of the item in the spinner.

Try this :

spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        Message mSelected = (Message) parent.getItemAtPosition(pos);
        Log.i("Id:", mSelected.getId());

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        Log.i("Message", "Nothing is selected");

    }


});
BSK-Team
  • 1,750
  • 1
  • 19
  • 37