java - Can not delete the note from the list and from the database in my note app for Android -
in note app android, if press long on note chose "delete" delete note, note still exists in list, after close app return it, note gone!
- hint: uses
oncontextitemselected
method show "delete" option.
how can delete note list , database?!
the mainactivity class contains oncontextitemselected
method:
package com.twitter.i_droidi.mynotes; import android.app.alertdialog; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.contextmenu; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; import java.util.list; public class mainactivity extends actionbaractivity implements adapterview.onitemclicklistener { listview lv; notesdatasource nds; list<notesmodel> noteslist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); nds = new notesdatasource(this); lv = (listview) findviewbyid(r.id.lv); nds.open(); noteslist = nds.getallnotes(); nds.close(); string[] notes = new string[noteslist.size()]; (int = 0; < noteslist.size(); i++) { notes[i] = noteslist.get(i).gettitle(); } arrayadapter<string> adapter = new arrayadapter<string>(mainactivity.this, android.r.layout.simple_list_item_1, android.r.id.text1, notes); lv.setadapter(adapter); registerforcontextmenu(lv); lv.setonitemclicklistener(this); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent nview = new intent(this, second2.class); nview.putextra("id", noteslist.get(position).getid()); nview.addflags(intent.flag_activity_clear_top); startactivity(nview); } @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_delete, menu); super.oncreatecontextmenu(menu, v, menuinfo); } @override public boolean oncontextitemselected(menuitem item) { switch (item.getitemid()) { case r.id.delete: nds.open(); nds.deletenote("id"); // check...!!! nds.close(); toast ndelete = toast.maketext(this, r.string.deleted, toast.length_long); ndelete.show(); } return super.oncontextitemselected(item); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.mainmenunewnote: intent nnote = new intent(this, second.class); startactivity(nnote); return true; case r.id.mainmenuabout: alertdialog.builder aboutdialog = new alertdialog.builder(this); aboutdialog.settitle(getstring(r.string.about_title)); aboutdialog.setmessage(r.string.about_body); aboutdialog.seticon(r.drawable.my_notes); aboutdialog.setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface aboutdialog, int witch) { // not anything. } }); aboutdialog.show(); return true; case r.id.mainmenuexit: alertdialog.builder exdialog = new alertdialog.builder(this); exdialog.settitle(r.string.exit_title); exdialog.setmessage(r.string.exit_body); exdialog.seticon(r.drawable.my_notes); exdialog.setnegativebutton(r.string.yes, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface exdialog, int which) { finish(); } }); exdialog.setpositivebutton(r.string.no, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface exdialog, int which) { // not anything. } }); exdialog.show(); return true; } return super.onoptionsitemselected(item); } }
the db class:
package com.twitter.i_droidi.mynotes; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class db extends sqliteopenhelper { private static final string db_name = "mynotes"; private static final int db_version = 1; public static final string table_name = "mynotes"; public static final string id = "id"; public static final string title = "title"; public static final string body = "body"; private static final string db_create = "create table " + table_name + " (" + id + " integer primary key autoincrement, " + title + " text not null, " + body + " text not null)"; public db(context context) { super(context, db_name, null, db_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(db_create); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } }
the notesdatasource class:
package com.twitter.i_droidi.mynotes; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.util.log; import java.util.arraylist; import java.util.list; public class notesdatasource { db mydb; sqlitedatabase sql; string[] getallcolumns = new string[]{db.id, db.title, db.body}; public notesdatasource(context context) { mydb = new db(context); } public void open() { try { sql = mydb.getwritabledatabase(); } catch (exception ex) { log.d("error in database!", ex.getmessage()); } } public void close() { sql.close(); } public void createnote(string title, string body) { contentvalues note = new contentvalues(); note.put(mydb.title, title); note.put(mydb.body, body); sql.insert(mydb.table_name, null, note); } public notesmodel getnote(int id) { notesmodel note = new notesmodel(); cursor cursor = sql.rawquery("select * " + db.table_name + " " + db.id + " = ?", new string[]{id + ""}); if (cursor.getcount() > 0) { cursor.movetofirst(); note.setid(cursor.getint(0)); note.settitle(cursor.getstring(1)); note.setbody(cursor.getstring(2)); cursor.close(); } return note; } public void updatenote(int id, string title, string body) { contentvalues note = new contentvalues(); note.put(mydb.title, title); note.put(mydb.body, body); sql.update(mydb.table_name, note, mydb.id + " = " + id, null); } public void deletenote(object id) { sql.delete(mydb.table_name, mydb.id + " = " + id, null); } public list<notesmodel> getallnotes() { list<notesmodel> noteslist = new arraylist<notesmodel>(); stringbuffer selectquery = new stringbuffer(); selectquery.append("select * "+ mydb.table_name +""); cursor cursor = sql.rawquery(selectquery.tostring(), null); if(cursor != null && cursor.movetofirst()) { { notesmodel notes = new notesmodel(); notes.setid(cursor.getint(0)); notes.settitle(cursor.getstring(1)); notes.setbody(cursor.getstring(2)); noteslist.add(notes); } while (cursor.movetonext()); } cursor.close(); return noteslist; } }
after removing item database remove deleted item
string[] notes
by using
notes.remove(position);
nd call
adapter.notifydatasetchanged();
Comments
Post a Comment