private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FaqFragment();
break;
case 1:
fragment = new AboutFragment();
break;
case2:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// set title
alertDialogBuilder.setTitle("Alert");
// set dialog message
alertDialogBuilder
.setMessage("Please select your choice")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//do whatever you want to do when user clicks ok
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
上面的代码我想知道一个特定的listview
项目,我需要显示带有yes或no按钮的对话框。但是在这里,我作为错误出现在线下,并且它没有显示一条语句。我该如何解决这个问题,请帮助我。...
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
答案 0 :(得分:0)
您遇到错误,因为您需要在案例编号之间添加空格,例如case 2:
例如:-
case 2:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// set title
alertDialogBuilder.setTitle("Alert");
// set dialog message
alertDialogBuilder
.setMessage("Please select your choice")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//do whatever you want to do when user clicks ok
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
break;