简体   繁体   中英

Android: Delete record row in a SQLIte database from a fragment

I have CardViews in a RecyclerView. Upon LongClick on a CardView, a DialogFragment opens to confirm deletion of the CardView and its data which comes from a SQLite database. Upon clicking "OK" the Dialog closes and the "deletefromDB" method in the DatabaseHelper file is set up to delete the record row from the Table in database. The method uses the CARDNUM column with its cardnum integer value to delete the record row of data. While the dialog closes as expected, unfortunately when I checked the database after running the app, nothing changed in the SQLite database (I am using DB Browser for SQLite to check the database before and after). What am I missing here?

DeleteCardViewFragment.java
public class DeleteCardViewFragment extends DialogFragment {

    DatabaseHelper dbHelper;
    private int cardnum = 0;


    public DeleteCardViewFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {

    dbHelper = new DatabaseHelper(getActivity());
    final View rootView = inflater.inflate(R.layout.delcardview_layout, container, false);
    getDialog().setTitle("Delete skycard");
    ...
    Button btnOK = (Button) rootView.findViewById(R.id.btnOK);
    btnOK.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            **dbHelper.deletefromDB(cardnum);**                
            dismiss();
        }
    });

    return rootView;
    }
} 

DBContract.java file for Database Table Name and Columns

public final class DBContract {
public DBContract() {}

public static abstract class DBEntry implements BaseColumns {
    public static final String TABLE_NAME_USERINPUTS = "userinputs";
    public static final String COLUMN_NAME_ID = "_id";
    public static final String COLUMN_NAME_CARDNUM = "cardnum";
    public static final String COLUMN_NAME_TODO = "todo";
    public static final String COLUMN_NAME_NOTE1 = "note1";
    public static final String COLUMN_NAME_NOTE2 = "note2";
    public static final String COLUMN_NAME_DUEDATE = "duedate";
    public static final String COLUMN_NAME_DUETIME = "duetime";
    public static final String COLUMN_NAME_TIMESTAMP = "timestamp";
    }
}

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
    ...
    private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE IF NOT EXISTS "+ DBContract.DBEntry.TABLE_NAME_USERINPUTS +
            "(" + DBContract.DBEntry.COLUMN_NAME_ID +
            " INTEGER PRIMARY KEY, " +
            DBContract.DBEntry.COLUMN_NAME_CARDNUM +
            " INTEGER," +
            DBContract.DBEntry.COLUMN_NAME_TODO +
            " TEXT," +
            DBContract.DBEntry.COLUMN_NAME_NOTE1 +
            " TEXT," +
            DBContract.DBEntry.COLUMN_NAME_NOTE2 +
            " TEXT," +
            DBContract.DBEntry.COLUMN_NAME_DUEDATE +
            " TEXT," +
            DBContract.DBEntry.COLUMN_NAME_DUETIME +
            " TEXT," +
            DBContract.DBEntry.COLUMN_NAME_TIMESTAMP +
            " INTEGER)";

    **public void deletefromDB(int cardnum)** {
          SQLiteDatabase db = this.getWritableDatabase();
          // Delete the CardView's data row using the integer from cardum.
          db.delete(DBContract.DBEntry.TABLE_NAME_USERINPUTS,
          DBContract.DBEntry.COLUMN_NAME_CARDNUM + "= ?", new String[]{String.valueOf(cardnum)});        
          db.close();
      }
  }

i think you are not passing the cardnum value from the hosting activity/fragment to your dialog so you always try to delete the cardnum=0 so the solution is you should pass the cardnum in a bundle and pass it as fragment argument in your dialog and get the value in the onCreateView() . in which your start your dialogfragment add this

Bundle args = new Bundle();
args.putInt("cardnum", cardnum);
myFragment.setArguments(args);

and in your onCreateView()

public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) {

dbHelper = new DatabaseHelper(getActivity());
final View rootView = inflater.inflate(R.layout.delcardview_layout, container, false);
getDialog().setTitle("Delete skycard");
// add this
cardnum = getArguments().getInt("cardnum", 0);
Button btnOK = (Button) rootView.findViewById(R.id.btnOK);
btnOK.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        dbHelper.deletefromDB(cardnum);               
        dismiss();
    }
});

return rootView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM