Android Studio-为什么所有内容都被删除?

时间:2018-12-25 13:32:22

标签: java sql database android-studio delete-row

对于我的大学,我必须创建一个ToDo-App。一种规范是您可以删除已完成的待办事项。

所以我可以进入应用程序并添加例如4个待办事项。然后删除其中两个并关闭应用程序。回到我的应用程序后,所有待办事项都将被删除。

如果我在关闭应用程序之前未删除任何待办事项,则当我再次打开它时,将显示所有待办事项。

我希望有人可以帮助我找到解决问题的方法。

这是我的代码:

public class ToDoList extends AppCompatActivity {

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


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

        db2 = new DataBaseToDo(this);

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

        myToDo = new ArrayList<String>();
        Cursor data = db2.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);
            }
        }
    }
// With click on the Button i want to delete the selected Items:
    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();
                    }
                }
        );
    }
}

这是我的数据库代码:

package com.vorlesung.iubh.mytodo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.SparseBooleanArray;

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 Cursor getListContents(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return data;
    }

    public void deleteAll() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("delete from "+ TABLE_NAME);
        db.close();
    }

    // I changes this part, but now there is a new problem .. see EDIT:
    public void deleteToDo(SparseBooleanArray posCheck) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE ID =  " + posCheck.keyAt(0));
        db.close();
    }
}

编辑

我稍微修改了代码,但是现在的问题是,它没有从数据库中删除任何选定的行。

0 个答案:

没有答案