用于确认删除行的对话框

时间:2012-11-04 16:18:03

标签: android dialog

我正在尝试添加一个对话框,当用户选择删除指定行的删除按钮时,该对话框会弹出。但是我似乎无法获得编译程序的相应语法。该行发生错误;

MODULEDATABASE.deleteRow(rowId);
Intent intent = new Intent(this, MyCourses.class);

我们非常感谢任何建议。

public class ViewCourse extends Activity implements OnClickListener{
Cursor cursor;
database MODULEDATABASE;
String rowId;
Button deleteModule;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_course);

        Intent intent = getIntent();
        rowId = intent.getStringExtra(MyCourses.TEST);
        MODULEDATABASE = new database(ViewCourse.this);
        MODULEDATABASE.openToRead(ViewCourse.this);
        cursor = MODULEDATABASE.getRow(rowId);

        TextView text_modulecode = (TextView)findViewById(R.id.viewModuleCode);
        TextView text_modulename = (TextView)findViewById(R.id.viewModuleName);

        text_modulecode.setText(cursor.getString(cursor.getColumnIndex(database.KEY_MODULECODE)));
        text_modulename.setText(cursor.getString(cursor.getColumnIndex(database.KEY_MODULENAME)));


        deleteModule = (Button)findViewById(R.id.deleteButton);
        deleteModule.setOnClickListener(this);
    }

    public void onClick (View deleteModule) 
    {
         Dialog(rowId);


    }

    public void Dialog (String rowId) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.confirmDelete)
               .setPositiveButton(R.string.confirmDelete, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                     MODULEDATABASE = new database(ViewCourse.this);
                     MODULEDATABASE.deleteRow(rowId);
                     Intent intent = new Intent(this, MyCourses.class);
                     startActivity(intent);

                   }
               })
               .setNegativeButton(R.string.confirmDelete, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                   }
               });
    }


}

1 个答案:

答案 0 :(得分:1)

您没有说出错误是什么,但我怀疑您需要更改此行:

Intent intent = new Intent(this, MyCourses.class);

为:

Intent intent = new Intent(ViewCourse.this, MyCourses.class);

(问题是,在代码中的那一点,this引用匿名OnClickListener类。)

编辑 - 将方法的rowId参数声明为final

public void Dialog (final String rowId) {
    . . .