Android listView doesn't get update when adding new items -
i'm trying create app can add strings list activity, every time add new string list replaces old one.
how can fix this?
code list is.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_show_list); button button = (button) findviewbyid(r.id.addbtn); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivity(new intent(showlist.this, addtolist.class)); } }); intent = getintent(); bundle b = i.getextras(); if(b!=null) { string texttolist = b.getstring("listtext"); list<string> stuff = new arraylist<string>(); final arrayadapter<string> adapter = new arrayadapter<>(this, android.r.layout.simple_list_item_1, stuff); listview list = (listview) findviewbyid(r.id.listview); list.setadapter(adapter); adapter.add(texttolist); adapter.notifydatasetchanged(); } }
the xml files code:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".showlist"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add" android:id="@+id/addbtn" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="list:" android:id="@+id/textview" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview" android:layout_centerhorizontal="true" android:layout_above="@+id/addbtn" android:layout_below="@+id/textview" />
code file input text:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add_to_list); final bundle b = new bundle(); final intent = new intent(addtolist.this, showlist.class); final edittext text = (edittext)this.findviewbyid(r.id.listtext); button btn = (button) findviewbyid(r.id.btnadd); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string s = text.gettext().tostring(); b.putstring("listtext", s); i.putextras(b); startactivity(i); } }); }
and xml code previous peace of code:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="com.example.mathias.listapp.addtolist"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listtext" android:layout_alignparenttop="true" android:layout_margintop="100dp" android:layout_alignparentright="true" android:layout_alignparentend="true" android:inputtype="text" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add" android:id="@+id/btnadd" android:layout_centervertical="true" android:layout_centerhorizontal="true" />
this happens because creating new list everytime enter in activity. field in save list has "permanent" one. recommend declare static list , check if it's not null:
code list is:
public class nameofmyclass{ static list<string> stuff; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_show_list); if(stuff==null){ stuff = new arraylist<string>(); } listview list = (listview) findviewbyid(r.id.listview); button button = (button) findviewbyid(r.id.addbtn); final arrayadapter<string> adapter = new arrayadapter<>(this, android.r.layout.simple_list_item_1, stuff); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivity(new intent(showlist.this, addtolist.class)); } }); intent = getintent(); bundle b = i.getextras(); if(b!=null) { string texttolist = b.getstring("listtext"); stuff.add(texttolist); } list.setadapter(adapter); } }
by way there several "problems" in code:
- you added item adapter, not list
- you initialized listview if bundle isn't null, why? listview remain everytime, don't need initialize check
- you created final intent in other class outside click. isn't programmatically wrong, there isn't motivation in case need it. it's better locally inside click did in first class
Comments
Post a Comment