我在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.
}
}
答案 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();