在我的应用程序中,我为Templates
提供了各种Text Message Body
,spinner list items
,可以选择,用户可以发送它们而不是键入message
,但问题是当用户打开菜单项以选择application crashes
模板时。 Spinner
我已放入alert dialog box
,可通过菜单项访问。
对话框代码*
AlertDialog.Builder rdialog = new AlertDialog.Builder(MainActivity.this);
rdialog.setTitle("Select Message");
rdialog.setIcon(android.R.drawable.ic_input_get);
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
alertView = inflater.inflate(R.layout.rptsetting,null);
final Spinner fSpinner = (Spinner)alertView.findViewById(R.id.fSpinner);
String providers[] ={"Busy", "Good Morning", "In office"};
ArrayAdapter<String> adp = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,providers);
fSpinner.setAdapter(adp);
fSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> aparent, View arg1,
int pos, long arg3) {
String selectedItem = fSpinner.getSelectedItem().toString();
if(selectedItem.equals("Busy")){
body = "Currently Busy call again later, Thanks";
}
if(selectedItem.equals("Good Morning")){
body = "A very Good Morning, Have a nice day";
}
if(selectedItem.equals("In office")){
body = "Currently in office";
}
}
@Override
public void onNothingSelected(AdapterView<?> aparent) {
}
});
rdialog.setView(alertView);
rdialog.setNeutralButton("SUBMIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog rdialog1 = rdialog.create();
rdialog1.show();
我将body
定义为全局字符串,以便Sms Manager
可以访问它以将其用作要发送的消息正文。
记录猫
提前致谢!
答案 0 :(得分:2)
首先在onItemSelected(.....)
String selectedItem = aparent.getItemAtPosition(pos).toString();
if(selectedItem.equals("Busy")){
body = "Currently Busy call again later, Thanks";
}
if(selectedItem.equals("Good Morning")){
body = "A very Good Morning, Have a nice day";
}
if(selectedItem.equals("In office")){
body = "Currently in office";
}
并交叉检查您的body
变量是否为空
答案 1 :(得分:1)
要从Spinner获取所选项目,请尝试使用getItemAtPosition
AdapterView
方法。为:
@Override
public void onItemSelected(AdapterView<?> aparent, View arg1,
int pos, long arg3) {
String selectedItem = aparent.getItemAtPosition(pos).toString();
//...your code...
}