Dialog box to confirm deletion of row


Ask by : Calgar99 August 24, 2015 14:23

I'm trying to add a dialog box which will pop up when a user selects a delete button which will delete a specified row. However I can't seem to get the appropriate syntax for the program to compile. The error occurs at this line;

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

Any suggestions would be much appreciated.

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

                   }
               });
    }


}
View original question

Answer by : Ted HoppNovember 04, 2012 16:20

You don't say what the error is, but I suspect you need to change this line:

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

to:

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

(The problem is that at that point in the code, this refers to the anonymous OnClickListener class.)

EDIT - Declare the rowId parameter to the method to be final:

public void Dialog (final String rowId) {
    . . .
View original answer