2 sachinks SachinKS 于 2013.04.11 14:16 提问

调用AlertDialog中的AlertDialog
public class Example
{
 String sp,st;
 SQLiteDatabase database, database2;
 void func()
 {
 new AlertDialog.Builder(view.getContext())
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("Confirm")
    .setMessage("Are you sure?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
    @Override
    public void onClick(DialogInterface dialog, int which) {
      String st = editTextSt.getText().toString();
      String sp = editTextSp.getText().toString();
      database2 = new SQLiteDbHelper(this);
      database = database2.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put(SQLiteDbHelper.COLUMN_NAME_SP, sp);
      values.put(SQLiteDbHelper.COLUMN_NAME_ST, st);
      long insertId = database.insert(SQLiteDbHelper.TABLE_NAME, null, values);
      //I want to call it here
    }})
    .setNegativeButton("No", null)
    .show();
 }

我想调用AlertDialog内部的AlertDialog:

new AlertDialog.Builder(this)
                .setMessage("Saved!")
                .setNeutralButton("OK", null)
                .show();

应该怎么实现?

如果我按照上面的代码,就会报错:** The constructor AlertDialog.Builder(new DialogInterface.OnClickListener(){}) is undefined **

2个回答

Momo1199
Momo1199   2013.04.11 15:21
已采纳

试试:

new AlertDialog.Builder(Example.this)
                .setMessage("Saved!")
                .setNeutralButton("OK", null)
                .show();

满足条件是类Example是一个activity

比如:

public class Example extends Activity

如果不是上面的情况,用这样:

new AlertDialog.Builder(view.getContext())
                .setMessage("Saved!")
                .setNeutralButton("OK", null)
                .show(); 
baimy1985
baimy1985   2013.04.11 18:30

最后那段代码中的this指的是(new DialogInterface.OnClickListener())而不是activity那个this,留意下指代对象就好了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!