在片段android中显示对话框

时间:2014-11-07 02:21:39

标签: android listview dialog fragment

我正在使用http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/教程来显示更精简的菜单并且运行良好。

现在我想知道一个特定的listview项目,我需要显示带有是或否按钮的对话框。所以我是新手。任何人都可以帮我解决这个问题吗?

这就是我的滑块菜单的样子,你可以看到如果我点击第三项我需要显示如图所示的对话框

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您正在学习本教程,那么请将MainActivity中的displayView(int position)方法更改为:

    private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new FindPeopleFragment();
        break;
    case 2:
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                MainActivity.this);

            // set title
            alertDialogBuilder.setTitle("Alert");

            // set dialog message
            alertDialogBuilder
                .setMessage("Pelase 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;
    case 3:
        fragment = new CommunityFragment();
        break;
    case 4:
        fragment = new PagesFragment();
        break;
    case 5:
        fragment = new WhatsHotFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}