旋转器和Android

时间:2011-04-15 18:11:55

标签: java android spinner

我想取一个微调器的值并将其转换为一个String来玩。

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

我认为这样的事情会起作用......

 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它没有......

我看了几个例子,但我不确定他们是否与我的问题直接相关。

提前致谢!

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:1)

嗯,我不完全确定问题是什么,但如果问题是获得微调器的价值,请查看Spinner data to string

  

您可以使用getSelectedItem   获取当前选定的项目。如果   你一定要去   ArrayAdapter&lt; String&gt;,这会   是价值。

当然,在这种情况下,你要返回CharSequence,所以你要做

String strVal = getSelectedItem().toString();

希望这有帮助,

Phil Lello