0

I have a problem. I try to delete the record from the database. I have no idea, but how to do it. I tried something like this, but not quite work. The basic gist is that I trying to delete from the database based on list item position, not database row ID. How delete base database row ID?

ListView listawszystkich;
public int pozycja;
DatabaseDEO db = new DatabaseDEO(this);
final String[] skad = new String[]{Database.tables.Transakcje.Kolumny.kwota, Database.tables.Transakcje.Kolumny.data, Kategorie.KOLUMNY.nazwa_kategorii, Database.tables.Transakcje.Kolumny.komentarz};
final int[] dokad = new int[]{R.id.kwotaglowna,R.id.dataglowna,R.id.kategoriaglowna,R.id.komentarzglowny};
private SimpleCursorAdapter adapterspinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_transakcje2);

    listawszystkich = (ListView) findViewById(R.id.listawszystkich);

    listawszystkich.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            pozycja = position+1;
        }
    });
    Cursor kursorwszystkie = db.wszystkietransakcje();
    adapterspinner = new SimpleCursorAdapter(getApplicationContext(), R.layout.activity_transakcje,kursorwszystkie,skad, dokad,0);
    adapterspinner.notifyDataSetChanged();
    listawszystkich.setAdapter(adapterspinner);
    registerForContextMenu(listawszystkich);

}



@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v.getId()==R.id.listawszystkich) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_list,menu);
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.delete:
            db.usuwanietransakcji(pozycja);
            Transakcje.this.recreate();
            Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
        default:
            return super.onContextItemSelected(item);
    }

Method to delete row (in DatabaseDEO):

    public void usuwanietransakcji(int id_transakcji){
    SQLiteDatabase db = DbHelper.getWritableDatabase();
    db.execSQL("DELETE FROM " + Transakcje.NAZWA_TABELI+ " WHERE "+Transakcje.Kolumny.id_transakcji+"='"+id_transakcji+"'");
    db.close();
}
9
  • Possibly a duplicate of stackoverflow.com/questions/7510219/… - Though what is DatabaseDEO? Commented Jan 6, 2017 at 20:57
  • Found it. DatabaseDEO is a custom class as posted by yourself in a previous question. stackoverflow.com/questions/41085675/… - In which case, definitely a duplicate. Use DbHelper.delete(). Commented Jan 6, 2017 at 20:59
  • Possible duplicate of Deleting Row in SQLite in Android Commented Jan 6, 2017 at 21:00
  • In DatabaseDEO I have method to delete record from database, but I would like to know how to download id_transakcji when the clicks on a particular elemnt listview. Cursor? But how? Commented Jan 6, 2017 at 21:01
  • Can you post DatabaseDEO in its current form? Commented Jan 6, 2017 at 21:07

1 Answer 1

1

Looks like you're trying to delete from the database based on list position rather than row ID:

db.usuwanietransakcji(pozycja);

I'm guessing id_transakcji is a unique value similar to a row ID? First you'll need to declare Cursor kursorwszystkie as a member variable outside of the onCreate method, but still populate it on the same line.

In onContextItemSelected() you'll need to query your cursor again:

case R.id.delete: {
        // Move to the selected row
        kursorwszystkie.moveToPosition(info.position);

        // Get the column ID of id_transakcji
        final int col_id = kursorwszystkie.getColumnIndex(Database.tables.Transakcje.Kolumny.id_transakcji);

        // Get id_transakcji
        final int id_transakcji = kursorwszystkie.getInt(col_id);

        // Query the database
        db.usuwanietransakcji(id_transakcji);
        Transakcje.this.recreate();
        Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
        break;
}

Note the addition of {} as I'm declaring a variable inside a switch case. Also add a break; at the end of the case so the code doesn't fall through into the default block. Let me know if that works.

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

3 Comments

I get error after click delete: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
That means it couldn't find the column. I was only guessing with Database.tables.Transakcje.Kolumny.id_transakcji as I don't know what columns you're using in DatabaseDEO. You'll need to replace it with the name of the column you've stored id_transakcji in, as used in DatabaseDEO.
Ok. I must correct my Cursor. Thank you for help. Good luck! ;)

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.