1

could any one help me that how to perform long click option in list view item's so i able to delete my notes from sq lite database ...i have 2 column in my note app they are id ,title and notetext

my code to create table is ..

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

long click code

lv.setOnItemLongClickListener(new OnItemLongClickListener(){
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
        data.remove(arg2);
        // adapter.notifyDataSetChanged();
        //adapter.notifyDataSetInvalidated();
        return true;
    }
});
HpTerm
  • 8,151
  • 12
  • 51
  • 67
raman rayat
  • 404
  • 5
  • 15

2 Answers2

2

yes i got the answer... it will be helpful for other stack flow user's :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 ;
}
HpTerm
  • 8,151
  • 12
  • 51
  • 67
raman rayat
  • 404
  • 5
  • 15
0

You only need to implement OnLongClickListener to your items and do the query to delete from your BBDD...

Android: Can't delete record from ListView and SQLite

Community
  • 1
  • 1
colymore
  • 11,776
  • 13
  • 48
  • 90
  • hey please check my code once ..i update it again with long click code – raman rayat Jan 05 '14 at 11:47
  • 01-05 08:05:36.683: E/InputEventReceiver(1630): Exception dispatching input event. 01-05 08:05:36.683: E/MessageQueue-JNI(1630): Exception in MessageQueue callback: handleReceiveCallback – raman rayat Jan 05 '14 at 13:06
  • 01-05 10:56:37.115: E/AndroidRuntime(861): FATAL EXCEPTION: main 01-05 10:56:37.115: E/AndroidRuntime(861):java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131230724, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)] 01-05 10:56:37.115: E/AndroidRuntime(861): at android.widget.ListView.layoutChildren(ListView.java:1545) 01-05 10:56:37.115: E/AndroidRuntime(861): – raman rayat Jan 05 '14 at 15:58
  • when i try to add adapter.notifyDataSetChanged(); adapter.notifyDataSetInvalidated(); it gave me compile time error (adapter can not be resolved ) – raman rayat Jan 05 '14 at 16:01