java - ArrayList / ArrayAdapter .add() function overwriting last element instead of adding to end of array -
i'm working on school assignment create bookmarking app 2 activites, listactivity called booknote , activity called manageactivity.
all typing must done in manageactivity , passed booknote list changes, reading, , writing data.
my problem when call function "addbookmark" in booknote activity. want happen new list item added @ end of list bookmark objects info in it.
what happening whatever item @ end of list overwritten instead of new item being created.
my code below comments stating intentions of each function.
booknote activity
public class booknote extends listactivity{ private arrayadapter<bookmark> adapter; private final string filename = "bookmarks.txt"; public string title = "empty", url = "empty", note = "empty"; public string bookmarkclicked =""; public int listviewid = 0; public boolean intentlistener = false; public int intentreturncounter = 0; public boolean addbookmarkclicked = false; public arraylist<bookmark> bookmarklist; /*when activity called, list populated readdata(), addbookmarkclicked reset false, intentlister changed true if returning manageactivity, if intentlistener true, addbookmarkclicked changed true if addbookmark clicked booknote menubar, bookmark object created information passed manageactivity, , if addbookmarkclicked true, bookmark object added end of list, otherwise inserted in same list element chosen edit.*/ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); bookmarklist = readdata(); adapter = new arrayadapter<bookmark>(this, android.r.layout.simple_list_item_1, bookmarklist); setlistadapter(adapter); addbookmarkclicked = false; intentlistener = false; intentlistener = getintent().getbooleanextra("listen", false); if (intentlistener == true) { addbookmarkclicked = getintent().getextras().getboolean("addbookmarkclicked", false); title = getintent().getextras().getstring("title"); url = getintent().getextras().getstring("url"); note = getintent().getextras().getstring("note"); listviewid = getintent().getextras().getint("listviewid"); bookmark bookmark = new bookmark(title, url, note); toast.maketext(getapplicationcontext(), "intent return: true", toast.length_short).show(); intentlistener = false; if (addbookmarkclicked == true) { addbookmark(bookmark); toast.maketext(getapplicationcontext(), "bookmark added", toast.length_short).show(); addbookmarkclicked = false; } else { insertbookmark(bookmark, listviewid); toast.maketext(getapplicationcontext(), "bookmark edited", toast.length_short).show(); } //writedata(); } } //reads data when app runs , fills arrayadapter , list items saved bookmarks.txt private arraylist<bookmark> readdata(){ arraylist<bookmark> bookmarks = new arraylist<>(); try { fileinputstream fis = openfileinput(filename); scanner scanner = new scanner(fis); if (scanner.hasnext()){ string titlescan = scanner.nextline(); string urlscan = scanner.nextline(); string notescan = scanner.nextline(); bookmark bookmark = new bookmark(titlescan, urlscan, notescan); bookmarks.add(bookmark); }else{ bookmark bookmark = new bookmark("example title", "example url", "example note"); bookmarks.add(bookmark); } scanner.close(); } catch (filenotfoundexception e) { } return bookmarks; } private void writedata(){ try { fileoutputstream fos = openfileoutput(filename, context.mode_private); outputstreamwriter osw = new outputstreamwriter(fos); bufferedwriter bw = new bufferedwriter(osw); printwriter pw = new printwriter(bw); for(int = 0; < adapter.getcount(); i++){ bookmark bookmark = adapter.getitem(i); pw.println(bookmark.gettitle() + "\n" + bookmark.geturl() + "\n" + bookmark.getnote()); } pw.close(); } catch (filenotfoundexception e) { log.e("write err", "cannot save: " + e.getmessage()); e.printstacktrace(); toast.maketext(booknote.this, "error saving", toast.length_short).show(); } } //if addbookmark menu item clicked, called add bookmark end of arrayadapter. private void addbookmark(bookmark bookmark){ adapter.add(bookmark); writedata(); } //calls manageactivity , reports information app. public void gotomanageactivity(boolean addbookmarkclicked){ intent manageintent = new intent(this, manageactivity.class); bundle extras = new bundle(); extras.putstring("bookmark", bookmarkclicked); extras.putboolean("addbookmarkclicked", addbookmarkclicked); extras.putint("listviewid", listviewid); manageintent.putextras(extras); startactivity(manageintent); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_add) { addbookmarkclicked = true; gotomanageactivity(addbookmarkclicked); return true; } return super.onoptionsitemselected(item); } }
manageactivity
public class manageactivity extends appcompatactivity { private string title = "empty", url = "empty", note = "empty"; private boolean listener = true; private boolean addbookmarkclicked = false; /* when activity starts, check addbookmarkclicked status, create book */ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.manage_layout); addbookmarkclicked = getintent().getextras().getboolean("addbookmarkclicked", false); string bookmarkstring = ""; bookmarkstring = getintent().getextras().getstring("bookmark"); if(bookmarkstring != null && bookmarkstring.length() > 0) { if (bookmarkstring.length() != 0) { string[] stringarray = bookmarkstring.split("\\n"); title = stringarray[0]; url = stringarray[1]; note = stringarray[2]; updatetextviews(); } } else { updatetextviews(); } } @override public void onbackpressed(){ intent booknoteintent = new intent(this, booknote.class); bundle extras = new bundle(); if(title.length() == 0 || title == null){title= "empty";} if(url.length() == 0 || url == null){ url = "empty";} if(note.length() == 0 || url == null) { note = "empty";} extras.putstring("title", title); extras.putstring("url", url); extras.putstring("note", note); extras.putboolean("listen", listener); extras.putboolean("addbookmarkclicked", addbookmarkclicked); booknoteintent.putextras(extras); startactivity(booknoteintent); }
great question! had deal issue myself well, couple of weeks ago.
the main thing need re-instantiate arrayadapter
, or adapter
in case.
now first step correctly:
bookmarklist = readdata(); adapter = new arrayadapter<bookmark>(this, android.r.layout.simple_list_item_1, bookmarklist); setlistadapter(adapter);
but again when trying update list: believe added new bookmark correctly, you'll need re-create arrayadapter listview.
edit: believe found peril. right here need re-instantiate arrayadapter, mentioned earlier.
private void addbookmark(bookmark bookmark){ adapter.add(bookmark); writedata(); }
basically, whenever update data shown in list, need step...
Comments
Post a Comment