长按方法从列表视图中删除项目

时间:2014-01-05 09:48:31

标签: android sqlite listview onlongclicklistener

  

任何人都可以帮我解决如何在列表中执行长按选项的问题   查看项目,所以我能够从sq lite数据库中删除我的笔记... i   在我的笔记应用中有2列,他们是id,title和notetext

我创建表的代码是..

mydb1 = Main1Activity.this.openOrCreateDatabase("185", MODE_PRIVATE, null);

mydb1.execSQL("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY AUTOINCREMENT,title varchar,notetext varchar);");

长按代码

lv.setOnItemLongClickListener(new OnItemLongClickListener(){
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
        data.remove(arg2);
        // adapter.notifyDataSetChanged();
        //adapter.notifyDataSetInvalidated();
        return true;
    }
});

2 个答案:

答案 0 :(得分:2)

是的,我得到了答案......它对其他堆栈流用户有用:D :)

public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
    ArrayAdapter <String> adapter = new ArrayAdapter<String>       
                                 (menu.this,android.R.layout.simple_list_item_1,data);
    Cursor cursor2=mydb1.rawQuery("SELECT * FROM notes;", null);

    data.remove(arg2);

    lv.setAdapter(adapter);

    cursor2.moveToPosition(arg2);
    int id= cursor2.getInt(cursor2.getColumnIndex("id"));

    mydb1.delete("notes", "id=?", new String[] {Integer.toString(id)});

    return true ;
}

答案 1 :(得分:0)

您只需要对项目实施OnLongClickListener并执行查询以从BBDD中删除...

Android: Can't delete record from ListView and SQLite