java - ListView doesn't appear in the MainActivty of my app (An image attached) -
after save note in android app, note (or listview) of note/s doesn't appear in mainactivity. mainactivity class of app is:
package com.twitter.i_droidi.mynotes; import android.app.alertdialog; 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.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; 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, second.class); nview.putextra("id", noteslist.get(position).getid()); // check...!!! 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(lv.getid()); // check...!!! nds.close(); toast ndelete = toast.maketext(this, "deleted.", toast.length_long); ndelete.show(); return true; } 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("about app"); aboutdialog.setmessage("the simplest app notes!\n\n" + "developed by: abdulaziz\n" + "twitter: @i_droidi\n" + "telegram: mrglitch\n\n" + "special tested app before upload on play store , use now! :)"); aboutdialog.seticon(r.mipmap.ic_launcher); aboutdialog.setpositivebutton("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("exit?"); exdialog.setmessage("are sure exit?"); exdialog.seticon(r.mipmap.ic_launcher); exdialog.setnegativebutton("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface exdialog, int which) { finish(); } }); exdialog.setpositivebutton("no", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface exdialog, int which) { // not anything. } }); exdialog.show(); return true; } return super.onoptionsitemselected(item); } }
the activity_main (xml/layout file) of app is:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <listview android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent"></listview> </linearlayout>
the second class of app is:
package com.twitter.i_droidi.mynotes; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.edittext; import android.widget.toast; public class second extends actionbaractivity { notesdatasource nds; edittext notetitle; edittext notebody; int id; db db; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.second); intent in = getintent(); id = in.getintextra("id", 0); notetitle = (edittext) findviewbyid(r.id.note_title); notebody = (edittext) findviewbyid(r.id.note); nds = new notesdatasource(this); nds.open(); notesmodel note = nds.getnote(id); nds.close(); notetitle.settext(note.gettitle()); notebody.settext(note.getbody()); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_second, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.secondmenusave: if (!notetitle.gettext().tostring().isempty() && !notebody.gettext().tostring().isempty()) { nds.open(); nds.updatenote(id, notetitle.gettext().tostring(), notebody.gettext().tostring()); nds.close(); toast nsave = toast.maketext(this, "saved.", toast.length_long); nsave.show(); finish(); } else { toast notsave = toast.maketext(this, "the title , content of note cannot empty!", toast.length_long); notsave.show(); } return true; case r.id.secondmenuback: alertdialog.builder badialog = new alertdialog.builder(this); badialog.settitle("back?"); badialog.setmessage("do want main page before saving note?"); badialog.seticon(r.mipmap.ic_launcher); badialog.setnegativebutton("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface badialog, int which) { finish(); } }); badialog.setpositivebutton("no", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface badialog, int which) { // not anything. } }); badialog.show(); return true; } return super.onoptionsitemselected(item); } }
the notesdatasource class of app is:
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>(); cursor cursor = sql.query(mydb.table_name, getallcolumns, null, null, null, null, null); cursor.movetofirst(); while (!cursor.isafterlast()) { notesmodel notes = new notesmodel(); notes.setid(cursor.getint(0)); notes.settitle(cursor.getstring(1)); notes.setbody(cursor.getstring(2)); noteslist.add(notes); cursor.movetonext(); } cursor.close(); return noteslist; } }
the db class of app is:
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); } }
a screenshot of app (mainactivity), after saving note (nothing shows!):
how can solve problem?!
or can write correct code?!
thanks helping me.
i think there 2 problem present in code
1) first replace
public list<notesmodel> getallnotes() { list<notesmodel> noteslist = new arraylist<notesmodel>(); cursor cursor = sql.query(mydb.table_name, getallcolumns, null, null, null, null, null); cursor.movetofirst(); while (!cursor.isafterlast()) { notesmodel notes = new notesmodel(); notes.setid(cursor.getint(0)); notes.settitle(cursor.getstring(1)); notes.setbody(cursor.getstring(2)); noteslist.add(notes); cursor.movetonext(); } cursor.close(); return noteslist; }
with these
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; }
2) goto second class on list item click this--
intent nview = new intent(this, second.class); nview.putextra("id", noteslist.get(position).getid()); // check...!!! nview.addflags(intent.flag_activity_clear_top); startactivity(nview);
and after adding note return mainactivity this----
intent nview = new intent(this, mainactivity.class); nview.addflags(intent.flag_activity_clear_top); startactivity(nview);
dont't use finish because end current task , take out previous screen in stack without refreshing
Comments
Post a Comment