Search code examples
android-studiosql-delete

Delete item with Button click


In my MainActivity I want my button to delete all items in my view list. Unfortunately, all the solutions I found here won't help me. If I click on the Button "Clear All" it deletes every item in my list, but the items are still saved in my Database.

I am totally new to Android Studio, so I don´t know how to write my code that it will work the way I want it.

This is my MainActivity:

package com.vorlesung.iubh.todo;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    DatabaseHepler myDB;
    ArrayList<String> list;
    ListView myToDoList;
    ArrayAdapter<String> arrayAdapter;
    SparseBooleanArray selectedItem;

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

        Button addToDo = (Button) findViewById(R.id.addToDo);
        Button clearAll = (Button) findViewById(R.id.clearALL);
        Button editSelected = (Button) findViewById(R.id.editSelected);
        Button clearDone = (Button) findViewById(R.id.clearDone);

        myToDoList = (ListView) findViewById(R.id.myToDoList);
        myDB = new DatabaseHepler(this);

        list = new ArrayList<>();
        Cursor data = myDB.getListContents();

        if (data.getCount() == 0)
            Toast.makeText(this, "The Database was empty",         Toast.LENGTH_LONG).show();
        else {
            while (data.moveToNext()) {
                list.add(data.getString(1));
                arrayAdapter = new ArrayAdapter<>(this,     android.R.layout.simple_list_item_multiple_choice, list);
                myToDoList.setAdapter(arrayAdapter);
            }
        }
    }

    public void onClickaddToDo(View button) {
        Intent newToDo = new Intent(this, AddToDo.class);
        startActivity(newToDo);
    }

    // With this onClick I want to delete all Items from my List.
    public void onClickaclearALL(View button) {
        list.clear();
        arrayAdapter.notifyDataSetChanged();
    }
}

And this is my DataBase:

package com.vorlesung.iubh.todo;

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

public class DatabaseHepler extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "myList.db";
    public static final String TABLE_NAME = "myList_Data";
    public static final String COL1 = "ID";
    public static final String COL2 = "ITEM1";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + "ITEM1 TEXT)";
        db.execSQL(createTable);
    }

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

    public boolean addData(String item1) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, item1);

        long result = db.insert(TABLE_NAME, null, contentValues);

        if(result == -1) {
            return false;
        } else {
            return true;
        }
    }

    public Cursor getListContents() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return data;
    }

    public void deleteRow() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_NAME+ " WHERE "+ COL2, null);
        db.close();
    }

}

Edit

There are the errors which are shown in my log File:

2018-12-23 10:16:05.627 21957-21973/com.vorlesung.iubh.todo E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2018-12-23 10:16:05.627 21957-21973/com.vorlesung.iubh.todo E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2018-12-23 10:16:05.628 21957-21973/com.vorlesung.iubh.todo E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2018-12-23 10:17:26.766 21957-21957/com.vorlesung.iubh.todo E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.vorlesung.iubh.todo, PID: 21957
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:5610)
    at android.view.View$PerformClick.run(View.java:22265)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:5610) 
    at android.view.View$PerformClick.run(View.java:22265) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
 Caused by: java.lang.IllegalArgumentException: Empty bindArgs
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1656)
    at com.vorlesung.iubh.todo.DatabaseHepler.deleteAllRow(DatabaseHepler.java:51)
    at com.vorlesung.iubh.todo.MainActivity.onClickaclearALL(MainActivity.java:59)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
    at android.view.View.performClick(View.java:5610) 
    at android.view.View$PerformClick.run(View.java:22265) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

Solution

  • You need to create a function in DatabaseHepler, to delete all the elements from table.

    In DatabaseHepler class Need to write this function.

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

    In your main activity need to write this code:

    // With this onClick I want to delete all Items from my List.
    public void onClickaclearALL(View button) {
        list.clear();
        myDB.deleteAllRow(); // this will delete all data from the table.
        arrayAdapter.notifyDataSetChanged();
    }
    

    This will help for you.