android - How to Pass all the data from listview in a activity -
i have 2 activities. first 1 consist of text view , edit text , button. edit text view , edit text inside list view. text view populate title server (i didn't populating data server yet, make dummy datas) , edit text data user user. once user fill edit text , press button datas activity passed activity. how make it? now, know on item click listener @ listview, that's not 1 want. how can datas listview?
1.main activity
public class mainactivity extends activity { listview listview; listadapter adapter; viewgroup group1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.listview1); string[] list = new string[]{ "title 1", "title 2", "title 3", "title 4", "title 5" }; adapter = new listadapter(this,list); listview.setadapter(adapter); group1 = (viewgroup) findviewbyid(r.id.mainlayout); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @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_settings) { return true; } return super.onoptionsitemselected(item); } static class listadapter extends baseadapter{ private context context; private string[] textstring; private layoutinflater vi; public listadapter(context context, string[] textstring){ this.context = context; this.textstring = textstring; vi = (layoutinflater) context.getsystemservice(layout_inflater_service); } @override public int getcount() { return textstring.length; } @override public object getitem(int position) { return textstring[position]; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; viewholder vh = new viewholder(); v = vi.inflate(r.layout.item_list_view, null); edittext et1 = (edittext) v.findviewbyid(r.id.edittext); et1.settag("et1" + position); vh.tv = (textview) v.findviewbyid(r.id.txttitle); vh.tv.settext(textstring[position]); return v; } class viewholder{ textview tv; } }
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainlayout" 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=".mainactivity"> <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/btn1"> </listview> <button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true"/>
activity_retrieve.xml
<textview android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp"/>
activity retrieve
public class retrieveactivity extends activity { textview txttemp; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_retrieve); txttemp = (textview) findviewbyid(r.id.text1); } }
inside listview adapter add onclicklistener() button used start activity.
first create arraylist array data saved,
arraylist<string> allarray = new arraylist<string>(); @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; viewholder vh = new viewholder(); v = vi.inflate(r.layout.item_list_view, null); edittext et1 = (edittext) v.findviewbyid(r.id.edittext); et1.settag("et1" + position); vh.tv = (textview) v.findviewbyid(r.id.txttitle); vh.tv.settext(textstring[position]); button b = (button) v.findviewbyid(r.id.btnsubmit); b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // user has click on button data edittext // , pass new activity allarray.add(et1.get(position).tostring()); intent = new intent (getapplicationcontext, retrieveactivity.class); i.putstringarraylistextra("edit_key_name", value); startactivity(i); } }); return v; }
now retrieveactivity class intent data this,
intent = getintent(); arraylist<string> passedtext = i.getstringarraylistextra("edit_key_name");
similarly can pass data as want.
Comments
Post a Comment