从ListView中删除项目会从SQLite数据库中删除另一个项目

时间:2015-02-08 10:59:15

标签: android sqlite listview simpleadapter

如果我从listView假设item_no = 5删除了某个项目,则会从id 5的数据库中删除其他项目。

这是我的代码。

我的数据库类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

我在listView中使用SimpleAdapterTRListForamat类包含所有getter和setter方法。删除按钮设置在AlertDialog上,单击listView项时弹出

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

我还需要从列表视图和数据库中删除相同的项目进行哪些更改?

是否可以使用SimpleAdapter完成,或者我需要使用SimpleCursorAdapter

3 个答案:

答案 0 :(得分:1)

使用SimpleAdapter时,行ID与位置SimpleAdapter.java#106相同。在您的情况下:remId始终为position + 1

为了获得更多控制权,我建议扩展BaseAdapter:

public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private List<TRListFormat> mList;

    public MyAdapter(Context context, List<TRListFormat> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 0;
        }
    }

    @Override
    public TRListFormat getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //todo create your view
        return null;
    }
}

现在OnItemClickListener将返回正确的id值。

修改

如果您不想使用BaseAdapter,可以覆盖SimpleAdapter getItemId方法。

首先将id添加到地图:

for (TRListFormat val : list) {
    HashMap<String, String> map = new HashMap<>();
    map.put("id", val.getId());
    ....
}

下一次覆盖getItemId(不是最佳解决方案,但它应该有效):

SimpleAdapter simpleAdapter = new SimpleAdapter(...) {
    @Override
    public long getItemId(int position) {
        return Long.valueOf(((Map<String,String>) getItem(position)).get("id"));
    }
};

答案 1 :(得分:1)

在您的TRDBHelper.class中添加此方法:

public int getItemIdByPosition(int position) {
     cursor.moveToPosition(position);
     return Integer.parseInt(cursor.getString(0));
}

在listView的onItemClickListener中使用该位置调用此方法,您将拥有良好的ID。

答案 2 :(得分:1)

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

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

并在SimpleAdapter中。

稍后您可以使用它来获取点击的项目

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