Android listview delete data but listview not update

636 views android
1

I delete data in SQLi but listview not update. I have to open activity agian then listview will update.

This is function delete data.

private void delData(int position) {


    cursor.moveToPosition(position);
    String rowId = cursor.getString(0);
    db.delete(DBHelper.TABLE_NAME, "_id = ?", new String[]{rowId});

    cursor.requery();
    adapter.notifyDataSetChanged();

}

This is function OnItemClick in OnCreate.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driver);

    dbHelper = new DBHelper(DriverActivity.this);

    lv_driver=(ListView)findViewById(R.id.lv_driver);
    lv_driver.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            delData(i);

        }
    });
}

How to update listview after delete data ?

answered question

You have to remove data even from your list's datasouece.

1 Answer

4

I delete data in SQLi but listview not update. I have to open activity agian then listview will update.

Yes, it's normal you are deleting the item from SQLite not from your ListView.

How to update listview after delete data ?

You can rebuild the ListView with new items

posted this

Have an answer?

JD

Please login first before posting an answer.