0

我在 上添加了数组,这些字符串在我运行应用程序时string.xml可见。Spinner问题是Toast不显示性别变量的值。

我为此制作了一个单独的课程,因为我还将Spinners用于birthmothbirthdatebirthyear

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    //SPINNERS
    spinner_gender = (Spinner) findViewById(R.id.reg_gender);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.array_gender, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_gender.setAdapter(adapter);
    spinner_gender.setOnItemSelectedListener(new MyOnItemSelectedListener());

    public class MyOnItemSelectedListener implements OnItemSelectedListener
{

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) 
    {
        genderString = parent.getItemAtPosition(pos).toString();
        Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG);
    }

    public void onNothingSelected(AdapterView<?> parent) 
    {
      // Do nothing.
    }
}
4

2 回答 2

1

Toast如果您不调用它的方法,则不会出现show()

Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
于 2012-06-24T05:56:20.873 回答
0

尝试这个..

Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();

或者..简单地你可以使用

Toast.makeText(getApplicationContext(), " Your Toast message in here",Toast.LENGTH_LONG).show();

于 2012-06-24T07:21:12.470 回答