如何根据ID而不是索引或位置在微调器上设置选择

时间:2014-03-12 07:15:47

标签: java android spinner

我有微调器,包含值和ID,如下所示:

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

这是在我的微调器上只显示值的方法:

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

这是我将Message传递给我的适配器的方式:

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

这是我从选定的微调器中获取ID的方式:

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

        }
}

问题是当我尝试根据ID: 165之类的ID设置我的微调器,而不是从值或索引时,我尝试使用此方法:

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

但它返回错误,所以我的问题是,如何根据他们的ID在微调器上设置选择?

3 个答案:

答案 0 :(得分:0)

使用此,

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

答案 1 :(得分:0)

oke我必须做同样的事情,我所做的是创建一个名为SpinnerPosition的额外int并将其添加到我的模型中(如Message类)

然后做了这样的事情:

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

    }
}

所以现在你可以做一些事情(如果你从某个地方获得id)

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

我很快写了代码,所以它可能会有一些错误,但我希望得到它背后的想法

答案 2 :(得分:0)

使用_id字段很容易。以下代码从微调游标中的_id字段中获取值,并输出该字段在光标中的位置(例如行号)

//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;
    }