Search code examples
androidsqlitelistviewsimpleadapter

Deleting item from ListView deletes some another item from SQLite database


If I delete an item from listView suppose item_no = 5, then some another item gets deleted from database whose id is not 5.

Here is my code.

My database class TRDBHelper.class

//Get single reminder
Cursor getReminder(int id){

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NAME, new String[]{COLUMN_ID, COLUMN_TITLE, COLUMN_DES,
                            COLUMN_DATE, COLUMN_TIME}, COLUMN_ID + "=?",
                            new String[]{String.valueOf(id)}, null, null, null, null);
    if(cursor != null)
        cursor.moveToFirst();

    return cursor;
}

//Get all reminders
public  List<TRListFormat> getAllReminders(){

    List<TRListFormat> remList = new ArrayList<>();

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);

    if(cursor.moveToFirst()){
        do {
            TRListFormat format = new TRListFormat();
            format.setId(Integer.parseInt(cursor.getString(0)));
            format.setTitle(cursor.getString(1));
            format.setDes(cursor.getString(2));
            format.setDate(cursor.getString(3));
            format.setTime(cursor.getString(4));

            remList.add(format);
        } while(cursor.moveToNext());
    }
    return remList;
}

//Delete single reminder
public void deleteReminder(int id){

    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME, COLUMN_ID + " = ?", new String[]{ String.valueOf(id)});
    db.close();
}

My list view TRList.class

I have use SimpleAdapter in listView and TRListForamat class has all getter and setter methods. Delete button is set on AlertDialog which pops when listView item is clicked

ListView lv;
SimpleAdapter adapter;
ArrayList<HashMap<String, String>> items;
List<TRListFormat> list;
TRDBHelper trDb;

public void refreshList() {
    items = new ArrayList<>();
    list = trDb.getAllReminders();

    for (TRListFormat val : list) {
        HashMap<String, String> map = new HashMap<>();
        map.put("title", val.getTitle());
        map.put("description", val.getDes());
        map.put("date", val.getDate());
        map.put("time", val.getTime());

        items.add(map);
    }
    adapter = new SimpleAdapter(this, items, R.layout.tr_list_format,
            new String[]{"title", "description", "date", "time"},
            new int[]{R.id.tbr_title, R.id.tbr_des, R.id.tbr_date, R.id.tbr_time});

    lv = (ListView) findViewById(R.id.tbr_list);
    lv.setAdapter(adapter);
}

new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

    int remId = (int)id + 1;  //id is a value from public void onItemClick(final AdapterView<?> adapterView, View view, final int position, final long id)  
                              //I also tried using position instead of id but both gives same result
    adapter.getItem(remId);
    adapter.getClass();

    Cursor rs = trDb.getReminder(remId);
    String id = rs.getString(rs.getColumnIndex(TRDBHelper.COLUMN_ID));
    String title = rs.getString(rs.getColumnIndex(TRDBHelper.COLUMN_TITLE));

    trDb.deleteReminder(Integer.parseInt(id));

    Toast.makeText(getApplicationContext(), "id: "+id+" title:"+title+" deleted", Toast.LENGTH_SHORT).show();

    refreshList();
}

What change do I need to delete same item from listview and database as well?

Can it be done with SimpleAdapter or I need to use SimpleCursorAdapter?


Solution

  • You can add and id in the item as hidden textview.

        map.put("id", val.getId());
    

    and in the SimpleAdapter.

    Later you can use this to get clicked item

    mListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
        String id = (String) obj.get("id");
    
        //delete remider by id
        trDb.deleteReminder(Integer.parseInt(id));
    }
    });