0

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

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) {

                   }
               });
    }


}
4

1 回答 1

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) {
    . . .
于 2012-11-04T16:20:55.873 回答