Android:从片段中删除SQLIte数据库中的记录行

时间:2016-07-16 04:30:37

标签: android database sqlite

我在RecyclerView中有CardViews。在LongView上打开CardView后,将打开DialogFragment以确认删除来自SQLite数据库的CardView及其数据。单击“确定”后,Dialog将关闭,DatabaseHelper文件中的“deletefromDB”方法将被设置为从数据库中的表中删除记录行。该方法使用CARDNUM列及其cardnum整数值来删除记录的数据行。虽然对话框按预期关闭,但不幸的是,当我在运行应用程序后检查数据库时,SQLite数据库中没有任何变化(我使用DB Browser for SQLite来检查数据库之前和之后)。我在这里缺少什么?

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文件

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();
      }
  }

1 个答案:

答案 0 :(得分:0)

我认为你没有将cardnum值从托管活动/片段传递到你的对话框,所以你总是试图删除cardnum = 0 所以解决方案是你应该在一个包中传递cardnum并将它作为片段参数传递给你的对话框并获取onCreateView()中的值。 你开始对话片段添加这个

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

并在你的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;
}