从数据库中删除ListView项目

时间:2018-12-27 17:51:39

标签: android database

单击一个按钮,我想从列表视图和数据库中删除选定的项目。从列表视图中删除可以正常工作,但是从我的数据库中删除将不起作用。

如何更改我的deleteToDo方法,该方法也会删除数据库中的行?

这是我的删除课程:

public class ToDoList extends AppCompatActivity {

    ListView lv;
    TextView tv;
    Button addToDo, delToDo, delDoneToDo;
    ArrayList<String> myToDo;
    ArrayAdapter<String> adapter;
    DatabaseHelper myDBH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do_list);

        myDBH = new DatabaseHelper(this);

        lv = (ListView) findViewById(R.id.ToDoList);
        tv = (TextView) findViewById(R.id.greeting);
        delDoneToDo = (Button) findViewById(R.id.delDoneToDo);
        delToDo = (Button) findViewById(R.id.delToDo);

        deleteData();
        deleteAllToDo();

        myToDo = new ArrayList<String>();
        Cursor data = myDBH.getListContents();

        if(data.getCount() == 0){
            Toast.makeText(getApplicationContext(), "No ToDo's available", Toast.LENGTH_SHORT).show();
        } else {
            while (data.moveToNext()){
                myToDo.add(data.getString(1));
                adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_multichoice, myToDo);
                lv.setAdapter(adapter);
            }
        }
    }

public void deleteData(){
        delDoneToDo.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SparseBooleanArray posCheck = lv.getCheckedItemPositions();
                        int count = lv.getCount();
                        for(int i=count-1; i>=0; i--){
                            if(posCheck.get(i)){
                                adapter.remove(myToDo.get(i));
                            } else {
                                Toast.makeText(getApplicationContext(), "No ToDo was selected", Toast.LENGTH_SHORT).show();
                            }
                        }
                        adapter.notifyDataSetChanged();
                        myDBH.deleteToDo(posCheck);
                        Toast.makeText(ToDoList.this, "To Do Deleted", Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }
}

这是我的数据库:

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "ToDo.db";
    public static final String TABLE_NAME = "todo_table";
    public static final String Col1 = "ID";
    public static final String Col2 = "ToDo_Title";
    public static final String Col3 = "Due_Date";
    public static final String Col4 = "Description";

    public DatabaseHelper (Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, ToDo_Title TEXT, Due_Date INTEGER, Description TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData (String ToDo_Title, String Due_Date, String Description) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Col2, ToDo_Title);
        contentValues.put(Col3, Due_Date);
        contentValues.put(Col4, Description);

        long result = db.insert(TABLE_NAME, null, contentValues);
        if(result == -1){
            db.close();
            return false;
    }

        else {
            db.close();
            return true;
        }
    }

    public void deleteToDo(SparseBooleanArray posCheck) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + Col1 + " = " + posCheck.keyAt(0));
        db.close();
    }
}

0 个答案:

没有答案