Android Spinner onItemSelected结果为textview?

时间:2012-06-25 21:25:15

标签: android spinner

我有一个微调器正在onItemSelected输出Toast。我希望它在textview中显示字符串值。那你怎么重写下面的方法来在一个名为denomiTV的文本视图中显示一个字符串:

    public void onItemSelected(AdapterView<?> parent, 
View view, int pos, long id) { 
Toast.makeText(parent.getContext()), "Your Selection is: " + 
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); 
} 

我试过这个,但Eclipse说.setText在这里不正确:

    public void onItemSelected(AdapterView<?> parent, 
View view, int pos, long id) { 
    denomiTV.setText(parent.getContext(), "Your selection is:" +
    parent.getItemAtPosition(pos));
    } 

3 个答案:

答案 0 :(得分:3)

setText()只需要一个CharSequence参数...您不应该传递Context

denomiTV.setText("Your selection is:" + parent.getItemAtPosition(pos));

答案 1 :(得分:0)

您不需要parent.getContext()作为TextView的参数,请查看TextView.setText(java.lang.CharSequence)

这就是你想要的:

denomiTV.setText("Your selection is:" + parent.getItemAtPosition(pos));

答案 2 :(得分:0)

试试此代码..

denomiTV.setText("Your Selection is : " + parent.getSelectedItem().toString());