0

I am deleting the row by calling the delete() method. It deletes from the database but the page doesn't update. Once I close the app and then open again or go to other page and come back then the row disappears from the screen . How do I to refresh the page after deleting a row from the database to show the updated data immediately?

mCursorAdapter.java

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list, parent, false);
}

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    final TextView rowId = (TextView)view.findViewById(R.id.rowId);
    final String rowNo = cursor.getString(cursor.getColumnIndex(Contract.Entry._ID));
    rowId.setText(rowNo);

    final TextView a = (TextView)view.findViewById(R.id.name);

    int nameColumnIndex = cursor.getColumnIndex(Contract.Entry.COLUMN_F_NAME);
    String name = cursor.getString(nameColumnIndex);

    a.setText(name);

    final CheckBox cb = (CheckBox)view.findViewById(R.id.c);

    cb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(cb.isChecked()) {
                a.setPaintFlags(a.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            } else {
                a.setPaintFlags(a.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));

            }
        }
    });
    final ImageView img = (ImageView)view.findViewById(R.id.summary);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("Do you want to DELETE the task?");
            builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    delete();
                    //notifyDataSetChanged();

                }

                private void delete() {

                    DbHelper mDbHelper = new DbHelper(context);
                    SQLiteDatabase sqLiteDatabase = mDbHelper.getWritableDatabase();

                    sqLiteDatabase.delete(Contract.Entry.TABLE_NAME,Contract.Entry._ID + "=?",new String[]{rowNo});
                    //Toast.makeText(context,"Data removed from row " + rowNo, Toast.LENGTH_SHORT).show();


                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(context,"No Button Clicked",Toast.LENGTH_SHORT).show();
                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();
        }
    });
 }
}
Basi
  • 3,009
  • 23
  • 28
Satya Routray
  • 85
  • 4
  • 10

1 Answers1

0

you have deleted record in DB, but you have to also update a data set in class, plus invoke .notifyDataSetChanged() on adapter.

see here

or here

you can notify adapter using broadcasts, or you could/should move your delete method to the adapter class.

something like this:

@Override
public void bindView(View view, final Context context, Cursor cursor) {

     HERE YOU MAKE YOUR DIALOG
}

//mopve your DELETE method outside of bindview
private void delete() {
    1.first delete record
    2.update collection in class
    3.invoke notifyDataSetChanged() on adapter
}
Lukas Novicky
  • 921
  • 1
  • 19
  • 44