如何使用数组从微调器中检索String?

时间:2012-06-24 05:51:43

标签: android arrays toast android-spinner

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

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

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.
    }
}

2 个答案:

答案 0 :(得分:1)

如果您没有在其上调用方法Toast,则不会显示show()

Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

答案 1 :(得分: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();