开发者

How to create a prompt dialogue

ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
 String[] items = {getResources().getString(R.string.menu_item_NxtInc)};
 ArrayAdapter<String> adapt = new ArrayA开发者_StackOverflowdapter<String>(this,R.layout.menu_item,items);
 menuList.setAdapter(adapt);
 menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
              TextView textView = (TextView) itemClicked;
              String strText = textView.getText().toString();
              if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_NxtInc))) {
                  startActivity(new Intent(EndActivity.this, NxtIncActivity.class));
                  EndActivity.this.finish();
              } 

I have created a menu in my application with the above code. If the user clicks on the string menu_item_NxtInc they are taken to the next activity. What i would like to happen is for a prompt to appear saying are you sure you want to do this? Anyone know how to implement this. thanks


As simple as this

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to do this baby?")
       .setCancelable(false)
       .setPositiveButton("Yuss, lets do this", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                // fire an intent go to your next activity
           }
       })
       .setNegativeButton("Err, no", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
AlertDialog alert = builder.create();
alert.show();


In Fragment in the place of this pass getActivity(). So it will become something like this.

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜