0

Been a little stuck here. I can delete the item in the listview but have problems deleting it from the database too:

My listview class:

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

        final VorgangDataSource dataSource = new VorgangDataSource(this);

        Log.d(TAG,"Die Datenquelle wird geöffnet!");
        dataSource.open();

        final List<vorgangsdaten> vorgangsdatenList = dataSource.getAllVorgangsDaten();

        final ArrayAdapter<vorgangsdaten> VorgangArrayAdapter = new ArrayAdapter<>(
                this,
                R.layout.mylistlayout,
                vorgangsdatenList
        );

        final ListView lv = (ListView)findViewById(R.id.listView);
        lv.setAdapter(VorgangArrayAdapter);
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                int pos = position;
                long i = id;
                vorgangsdatenList.remove(pos);
                dataSource.deleteRow(i);
                VorgangArrayAdapter.notifyDataSetChanged();

                return false;
            }
        });
        dataSource.close();



        //ActionBar Costumization
        android.support.v7.app.ActionBar ab = getSupportActionBar();
        ab.setIcon(R.drawable.search1);
        ab.setDisplayShowHomeEnabled(true);
        ab.setDisplayUseLogoEnabled(true);
    }
}

deleterow-method:

public void deleteRow(long id){
        String s_id;
        s_id = String.valueOf(id);
        database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + "="+s_id,null);
    }

table-rows from vorgangdbhelper:

  public static final String TABLE_VORGAENGE_LIST = "vorgaenge_list";

    public static final String COLUMN_ID = "_id";

My deleteRow gives me a d`oh. So what am I doing wrong?

EDIT: ErrorMessage

05-29 21:43:55.829 7725-7725/com.example/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example PID: 7725
                                                                              java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example/databases/vorgaenge_list.db
                                                                                  at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
                                                                                  at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1613)
                                                                                  at com.example.VorgangDataSource.deleteRow(VorgangDataSource.java:75)
                                                                                  at com.example.anzeigen$1.onItemLongClick(anzeigen.java:71)
                                                                                  at android.widget.AbsListView.performLongPress(AbsListView.java:3659)
                                                                                  at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3601)
                                                                                  at android.os.Handler.handleCallback(Handler.java:733)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:146)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5679)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
                                                                                  at dalvik.system.NativeStart.main(Native Method)

SOLUTION:

deleteRow-Method:

public void deleteRow(long id){
        String s_id;
        s_id = String.valueOf(id);

        database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + " = ?", new String[] { s_id });
    }
3
  • What's the error message that get's thrown? Commented May 29, 2016 at 19:15
  • Please try to move line dataSource.close() inside the onDestroy() method instead. The problem is that you close the database in onCreate. Commented May 29, 2016 at 19:45
  • Errors in initial post Commented May 29, 2016 at 19:52

1 Answer 1

2

Please try to move line dataSource.close() inside the onDestroy() method instead. The problem is that you close the database in onCreate().

And also update the delete method to: database.delete(VorgangDbHelper.TABLE_VORGAENGE_LIST,VorgangDbHelper.COLUMN_ID + " = ?", new String[] { s_id });

Sign up to request clarification or add additional context in comments.

8 Comments

OK that doesn´t create a d´oh and I can delete the listview again but the database entry is still there when I get out of my listview activity and back in.
What type is your dataSource object? You may have to "commit" or "flush" the change..
final VorgangDataSource dataSource = new VorgangDataSource(this); it has all the info about columns. if you need it I can post it in the initial post
Yes I can see that, but "VorgangDataSource" is not a public class. Please look inside it - what's the base/super class in this case?
It is a public class :-) adding it now to init post
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.