删除从ListView项将删除SQLite数据库的一些其他项目 [英] Deleting item from ListView deletes some another item from SQLite database

查看:175
本文介绍了删除从ListView项将删除SQLite数据库的一些其他项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从的ListView 删除项目料想 ITEM_NO = 5 ,然后一些其他项目会从数据库的删除 ID 不可以 5。

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

下面是我的code。

我的数据库类 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();
}

我的列表视图 TRList.class

我用 SimpleAdapter 在ListView和 TRListForamat 类有所有的getter和setter方法​​。删除按钮被设置在 AlertDialog 会弹出当的ListView 项被点击

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?

是不是可以用 SimpleAdapter 做的或者我需要使用 SimpleCursorAdapter

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

推荐答案

您可以在项目中添加和ID作为隐藏的TextView。

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

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

和在SimpleAdapter

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));
}
});

这篇关于删除从ListView项将删除SQLite数据库的一些其他项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆