[英]how to show tne number of selected item of spinner
Where is the issue in code? 代码中的问题在哪里? I want to show in Toast the number of selected item of spinner.
我想在Toast中显示微调器的选定项目数。 When I use num method the app log out me.
当我使用num方法时,该应用程序将我注销。
public class MainActivity extends AppCompatActivity {
String[] dataA = {"Choose type of goal",
"Up to 5",
"Up to 15",
"Up to 23",
"Up to 25"
};
int num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setadapter();
}
public void setadapter() {
ArrayAdapter<String> adapterA = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataA);
adapterA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinnerA = (Spinner) findViewById(spinner);
spinnerA.setAdapter(adapterA);
}
public int getNum() {
Spinner spinnerA = (Spinner) findViewById(spinner);
num = spinnerA.getSelectedItemPosition();
return num;
}
public void num(View v) {
Toast.makeText(getBaseContext(), getNum(), Toast.LENGTH_SHORT).show();
}
}
You are using the int
value of getNum()
which is interpreted by the system as a resID
! 您正在使用
getNum()
的int
值,该值被系统解释为resID
! You should convert your getNum()
to a String by using String.valueOf(getNum())
您应该使用
String.valueOf(getNum())
将getNum()
转换为字符串。
Side Note: You should make your Spinner a global variable and access it in that method instead of recreating the object each time. 旁注:您应该将Spinner设置为全局变量,并使用该方法访问它,而不是每次都重新创建对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.