java - How to add an item to a ListView with each Button click -
this code i'm working on, should add item in listview (w/ array) each time button clicked. text inserted user on edittext should added list each time button clicked.
i find line of code:
new button.onclicklistener()
is seen 'anonymous' thing, , it's not run, please me out ;(
import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.*; import android.widget.*; import java.lang.*; import java.util.list; public class mainactivity extends actionbaractivity { //setup lists string[] arraynames; listview listnames; textview namestext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //creating , printing lists listnames = (listview) findviewbyid(r.id.listnamesid); namestext = (textview) findviewbyid(r.id.namestexter); final arrayadapter<string> adapternames = new arrayadapter<string>(this,android.r.layout.simple_list_item_1, android.r.id.text1, (list<string>) namestext); button buttonplus = (button)findviewbyid(r.id.buttonplus); buttonplus.setonclicklistener( new button.onclicklistener() { public void onclick(view v) { listnames.setadapter(adapternames); } } ); }
xml
<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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <listview android:id="@+id/listnamesid" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#000" android:dividerheight="1dp" android:headerdividersenabled="true" android:layout_margintop="50dp"></listview> <button style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/textnametextname" android:id="@+id/buttonplus" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:nestedscrollingenabled="false" /> <edittext android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/namestexter" android:layout_alignbottom="@+id/buttonplus" android:layout_centerhorizontal="true" android:inputtype="text" />
the problem not button click listener, it's you're not using list data source of adapter.
i got working using arraylist data source, , updating in button click listener. each time click button, whatever in edittext gets added listview.
public class mainactivity extends actionbaractivity { arraylist<string> arraynames = new arraylist<string>(); listview listnames; textview namestext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //creating , printing lists listnames = (listview) findviewbyid(r.id.listnamesid); namestext = (textview) findviewbyid(r.id.namestexter); final arrayadapter<string> adapternames = new arrayadapter<string>(this,android.r.layout.simple_list_item_1, android.r.id.text1, arraynames); listnames.setadapter(adapternames); button buttonplus = (button)findviewbyid(r.id.buttonplus); buttonplus.setonclicklistener( new button.onclicklistener() { @override public void onclick(view v) { arraynames.add(0, namestext.gettext().tostring()); adapternames.notifydatasetchanged(); namestext.settext(""); } } ); }
Comments
Post a Comment