2

I have spinner, that containing value and ID , such as below :

ID      message
---     ----
100     Elephant
120     Lion
125     Cat
153     Dog
165     Bear

this is the way for showing only value on my spinner :

    public class Message {
private Int id;
private String message;
public Int getId() {
    return id;
}
public void setId(Int id) {
    this.id = id;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}

 @Override
 public String toString() {          
     return  message;
 }

this is how I passing Message into my adapter :

 protected List<Message> messages = null;
ArrayAdapter <Message> arrayadapter = new ArrayAdapter<Message>( activity,android.R.layout.simple_spinner_item,messages);               
                    arrayadapter.setDropDownViewResource(R.layout.textview);

spinner1.setAdapter(arrayadapter);

this is how i get ID from selected spinner :

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

        }
}

the problem is when I tried to set my spinner based on ID such as ID: 165, not from value or index, I tried to this method :

ArrayAdapter myAdap = (ArrayAdapter) spinner1.getAdapter();
        int spinnerPosition = myAdap.getPosition(message.getID());
        spinner1.setSelection(Message.getID());

but it return error, so my question, how to set selection on spinner based on their ID?

Menma
  • 799
  • 1
  • 9
  • 35
  • i think you should check [this link](http://stackoverflow.com/questions/2390102/how-to-set-selected-item-of-spinner-by-value-not-by-position) once. – Kunu Mar 12 '14 at 07:21

3 Answers3

0

use this,

 spinner2.setSelection(getIndex(spinner2, Message.getID()));
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                String CandidateNos = spinner2.getSelectedItem().toString();
                System.out.println("Spinner 2:"+CandidateNos);


            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });



 private int getIndex(Spinner spinner, int myString)
 {
  int index = 0;

  for (int i=0;i<spinner.getCount();i++){
   if (Integer.parseInt(spinner.getItemAtPosition(i).toString())==myString){
    index = i;
    i=spinner.getCount();//will stop the loop, kind of break, by making condition false
   }
  }
  return index;
 } 
Karthick pop
  • 616
  • 3
  • 16
0

oke I had to do the same thing what i did is made an extra int called SpinnerPosition and added that to my model (like your Message class)

then did something like this:

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

    }
}

so now you can do something like (if you get the id from somewhere)

ArrayAdapter myAdap = (ArrayAdapter) spinner1.getAdapter();
         if (Message.getID() == id) {
        previousSelected = Message.getSpinnerPosition();
        int spinnerPosition = myAdap.previousSelected;
        spinner1.setSelection(spinnerPosition);
}

i quickly wrote the code so it probably is going to have some mistakes but I hope get get the idea behind it

kicked11
  • 117
  • 8
  • what is type data from ID? – Menma Mar 13 '14 at 02:28
  • it's an int since Message.getID is an int. but the id is something you pass from somewhere. The basic idea is you store the position in the spinner of a certain message with a certain id in your message object as well. So now you can look up a message by id and then get the position in the spinner out of the same object. I hope this is clear? – kicked11 Mar 13 '14 at 06:22
0

It's easy if you use the _id field. The below code takes the value from the _id field in a Spinner Cursor and outputs the position of that field in the curser (e.g. row number)

//in onCreate or wherever...  iID = your chosen ID.  myOrder = the position to set the spinner
 Spinner mySpinner =(Spinner)findViewById(R.id.someSpinner);
 int iID = 5;  //find the index for the value #5 in your cursor
 int myOrder = getSpinnerIndex(mySpinner,iID);
 mySpinner.setSelection(myOrder);


 //function we are calling from above code.  put it elsewhere in your class.
    private int getSpinnerIndex(Spinner spinner, int mySetId)
    {
        int index = 0;
        for (int i=0;i<spinner.getCount();i++){
            if (spinner.getItemIdAtPosition(i)==mySetId){
                index = i;
                i=spinner.getCount();
            }
        }
        return index;
    }
Vette
  • 511
  • 5
  • 10