Terran
Terran

Reputation: 1

I cant delete some rows by id in SQLite

I always cant delete first two sometimes three records. They are on Listview, when you press element you will see delete button on another layout. On Log im getting correct index for every element.

So here is my code:

Main Activity:

        viewOfT.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent i=new Intent(MainActivity.this, popupWindow.class);
                i.putExtra("index",id);
                startActivity(i);
            }
        });         
}

public void populateListView() {


    Cursor data = db.getData();
    ArrayList<String> listData = new ArrayList<>();
    while(data.moveToNext()){


               k.setId(data.getInt(0));
               k.setTask(data.getString(1));

        listData.add("- " + k.getTask());


    }

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            listData);

    viewOfT.setAdapter(arrayAdapter);
    arrayAdapter.notifyDataSetChanged();
    viewOfT.invalidateViews();

Delete button in other activity:

  del.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        Bundle bundle=getIntent().getExtras();
        long value=bundle.getLong("index");

        db.deleteRecord(value);

        finish(); }
});

And SQLHelper:

public void deleteRecord(long id) {
    SQLiteDatabase db = this.getWritableDatabase();

    db.delete(TABLE_NAME, KEY_ID + "=" + id, null);
    close();
}

Upvotes: 0

Views: 136

Answers (1)

MikeT
MikeT

Reputation: 57043

1) Delete populateListView method

2) Add the following as class variables:-

SimpleCursorAdpater sca;
Cursor data;

3) in the onCreate method add :-

data = getData();
sca = new SimpleCursorAdapter(
    this,        // Context same as for array adapter
    android.R.layout.simple_list_item_1, // layout to be used
    data, // <<<<<<<< the cursor to be used for the list
    new String[]{"columnname_to_be_displayed"}, // <<<<<<<< cursor column to display name
    new int[android.R.id.text1], // the view into which the cursor data will be placed
    0 // a flag 0 is fine
);

viewOfT.setAdapter(arrayAdapter);

4) Add a new method to override the 'onResume' method (or alter it if already overridden) :-

@Override
protected void onResume() {
    super.onResume();
    data = getData();
    sca.swapCursor(); // can you sca.notifyDatasetChanged()
}
  • As you are calling another activity to delete the task, onResume will be called when returning from the other activity so the data is again retrieved from the database (deleted row will not exist) and the adpater is told to refresh the data.

You should ideally also override the onDestroy() method to close the cursor (data.close();)


Important Consideration

A cursor column named _id must exist for CursorAdapters (that's how the SimpleCursorAdapter knows what pass to the onItemClickListener).

if KEY_ID does not equate to _id; you either need to change KEY_ID to _id or amend the getData() method to include the _id column (which should be the value of the row's identifier) e.g. assuming a very basic query:

public Cursor getData() {
    return db.query(TABLE_NAME,"rowid AS _id, *",null,null,null,null,null);
} // Note! will add an extra column so beware if using column offsets

or perhaps :-

public Cursor getData() {
    return db.query(TABLE_NAME,KEY_ID + " AS _id, *",null,null,null,null,null);
} // Note! will add an extra column so beware if using column offsets

A Note on Column offsets

In your code you have :-

           k.setId(data.getInt(0));
           k.setTask(data.getString(1));

0 and 1 are column offsets (and could change e.g. the two alternative getData() methods). As such it's generally better to take advantage of the Cursor method getColumnIndex(columnname) e.g. the above could be :-

           k.setId(data.getInt(data.getColumnIndex(KEY_ID));
           k.setTask(data.getString(data.getColumnIndex("TASK_COLUMN")));

Note! not that you will need to create an Array as the SimpleCursorAdpater takes the cursor as the source. (KEY_ID would likely have to be prefixed with the DatabaseHelper Class).

Upvotes: 1

Related Questions