开发者

Android - How to add selection from Spinner to EditText

what I'm trying to do is make a selection from a spinner in android and then whatever is selected to be added to an开发者_运维百科 edittext box. The code I have so far is this...

spinner.setOnItemSelectedListener(
          new OnItemSelectedListener() {

     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

      edittext.setText("");

     }

     public void onNothingSelected(AdapterView<?> arg0) {
      // TODO Auto-generated method stub

     }
              //add some code here
          }
      );

Problem is this seems to be run even before the spinner is selected so it always sets my edittext to "". Ideally I would like to have it set the text to the selection made in the spinner. So, anyone have any ideas?


At startup, your spinner will get its defaultvalue, that counts as a selection. Do a boolean FirstTime or something like that.

You probably initialize your spinner from some array or something? The function actually looks like this

public void onItemSelected(AdapterView<?> parent,
    View view, int position, long id);

So just use the position variable

{
    edittext.setText(myArray[position]);
}


You can use the getItem method in the adapter to get the object that is shown. Like this:

onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  editText.setText((String) adapter.getItem(position));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜