开发者

Spinners and Android

开发者 https://www.devze.com 2023-02-25 08:47 出处:网络
I would like to take the value of a spinner and convert it to a String to play about with. Spinner s1 = (Spinner)findViewById(R.id.spinner1);

I would like to take the value of a spinner and convert it to a String to play about with.

    Spinner s1 = (Spinner)findViewById(R.id.spinner1);
    s1.setOnItemSelectedListener((OnItemSelectedListener) this);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.languages, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter);

I thought somthing like this would work....

 private OnClickListener sendClickListener = new OnClickListener(){
    public void onClick(View arg0) {
        EditText dstName = (EditText) findViewById(R.id.destinationAddress);
        EditText dstLanguage = (EditText) findViewById(R.id.spinner1);

        String address = dstName.getText().toString();   
        String language = dstLanguage.getText().toString();

        ops.createSocketConnection(language, address);  
        Notification notification = new Notification();
        notification.vibrate = new long[] {100}开发者_开发问答;
    }};

Allas it does not....

I have looked at a few examples however I am not sure If they are directly related to my question.

Thanks in advance!


Try this:

OnItemSelectedListener itemSelectedHandler = new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {
        String str = parent.getItemAtPosition(pos).toString();
        // Do whatever you want with the string
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // Do nothing
    }
};
//set the spinner's listener for select event

mSpinner.setOnItemSelectedListener(itemSelectedHandler);


Hmm, I'm not entirely sure what the question is, but if the issue is getting the value of a spinner, check out Spinner data to string:

You can use getSelectedItem to get the currently selected item. If you've bound to an ArrayAdapter<String>, this will be the value.

Of course, in this instance you're returning CharSequence, so you'd do

String strVal = getSelectedItem().toString();

Hope this helps,

Phil Lello

0

精彩评论

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