java - adding item to listView after passing info through an intent in Android -
java - adding item to listView after passing info through an intent in Android -
i trying add together item, first using add together button, going different activity, coming original 1 , adding in listview. can't seem have more 1 item.
addscreen.class (my first activity):
bundle com.painlogger; **imports** public class addscreen extends activity implements onclicklistener, onitemclicklistener { /** called when activity first created. */ button addbutton; simpleadapter adapter; list<hashmap<string, string>> painitems = new arraylist<hashmap<string, string>>(); listview listthings; int[] to; string[] from; string painlevelstring, timeofpainstring, texttreatmentstring, painlocation, row1, row2; textview paintitle; boolean myfirsttime; int k; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.addscreen); // if it's first time opening app, go 'addmyinfo.class' sharedpreferences settings = this.getsharedpreferences("myapp", 0); sharedpreferences.editor e = settings.edit(); boolean firstrun = settings.getboolean("firstrun", true); if (firstrun) { e.putboolean("firstrun", false); e.commit(); intent intent = new intent(this, addmyinfo.class); startactivity(intent); } //initialize paintitle , set font paintitle = (textview) findviewbyid(r.id.paintitle); typeface font = typeface.createfromasset(getassets(), "chantelli_antiqua.ttf"); paintitle.settypeface(font); listthings = (listview) findviewbyid(r.id.listthings); = new string[] { "row_1" + painitems.size(), "row_2" + painitems.size() }; = new int[] { r.id.row1, r.id.row2 }; adapter = new simpleadapter(this, painitems, r.layout.mylistlayout, from, to); listthings.setadapter(adapter); listthings.setonitemclicklistener(this); addbutton = (button) findviewbyid(r.id.addbutton); addbutton.setonclicklistener(this); } @override //on activityresult,get string extra, add together item list protected void onactivityresult(int requestcode, int resultcode, intent data) { if(this.getintent().hasextra("row1")){ row1 = this.getintent().getstringextra("row1"); row2 = this.getintent().getstringextra("row2"); painlevelstring = this.getintent().getstringextra("com.painlogger.painlevel"); painlocation = this.getintent().getstringextra("painlocation"); timeofpainstring = this.getintent().getstringextra("com.painlogger.paintime"); texttreatmentstring = this.getintent().getstringextra("com.painlogger.treatment"); additem(); } } // add together item, set in map, , add together map list private void additem() { hashmap<string, string> map = new hashmap<string, string>(); map.put("row_1" + painitems.size(), row1); map.put("row_2" + painitems.size(), row2); painitems.add(map); adapter.notifydatasetchanged(); } public void onclick(view v) { // when '+' clicked, go painloggeractivity.java, can // click enter, , sends strings here, can // incorporate them list view, same there in // painlogger activity intent gotofields = new intent (this, painloggeractivity.class); //put desired extras intent startactivityforresult(gotofields, 1); } public void onitemclick(adapterview<?> a, view v, int position, long id) { intent intent = new intent(this, item1.class); intent.putextra("com.painlogger.painlevel", painlevelstring); intent.putextra("com.painlogger.paintime", timeofpainstring); intent.putextra("com.painlogger.treatment", texttreatmentstring); intent.putextra("painlocation", painlocation); startactivity(intent); } }
my painloggeractivity, come in various info set list:
package com.painlogger; // imports public class painloggeractivity extends activity implements onclicklistener, onitemclicklistener, onkeylistener {
/** called when activity first created. */ edittext txtitem, txtitem2, timeofpain, texttreatment, painlevel; button btnadd; listview listitems; arrayadapter<string> aa; list<hashmap<string, string>> painitems = new arraylist<hashmap<string, string>>(); int[] to; string[] from; simpleadapter adapter; string timeofpainstring, texttreatmentstring, painlevelstring, painlocation; spinner s; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); txtitem = (edittext) findviewbyid(r.id.txtitem); txtitem2 = (edittext) findviewbyid(r.id.txtitem2); timeofpain = (edittext)findviewbyid(r.id.timeofpain); texttreatment = (edittext)findviewbyid(r.id.texttreatment); painlevel = (edittext)findviewbyid(r.id.painlevel); btnadd = (button) findviewbyid(r.id.btnadd); listitems = (listview) findviewbyid(r.id.listitems); btnadd.setonclicklistener(this); = new string[] { "row_1", "row_2" }; = new int[] { r.id.row1, r.id.row2 }; adapter = new simpleadapter(this, painitems, r.layout.mylistlayout, from, to); listitems.setadapter(adapter); listitems.setonitemclicklistener(this); s = (spinner) findviewbyid(r.id.spinner1); arrayadapter<?>adapter = arrayadapter.createfromresource( this, r.array.planets, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); s.setadapter(adapter); } private void additem() { timeofpainstring = timeofpain.gettext().tostring(); texttreatmentstring = texttreatment.gettext().tostring(); painlevelstring = s.getselecteditem().tostring(); painlocation = txtitem.gettext().tostring(); intent intent = new intent (this, addscreen.class); intent.putextra("com.painlogger.painlevel", painlevelstring); intent.putextra("com.painlogger.paintime", timeofpainstring); intent.putextra("com.painlogger.treatment", texttreatmentstring); intent.putextra("painlocation", painlocation); setresult(1, intent); finish(); hashmap<string, string> map = new hashmap<string, string>(); map.put("row_1", txtitem.gettext().tostring()); map.put("row_2", txtitem2.gettext().tostring()); painitems.add(map); adapter.notifydatasetchanged(); } public void onclick(view v) { if (v == this.btnadd) { additem(); } } public boolean onkey(view v, int keycode, keyevent event) { if (event.getaction() == keyevent.action_down && keycode == keyevent.keycode_dpad_center) { this.additem(); } homecoming false; } public void onitemclick(adapterview<?> a, view v, int position, long id) { timeofpainstring = timeofpain.gettext().tostring(); texttreatmentstring = texttreatment.gettext().tostring(); painlevelstring = s.getselecteditem().tostring(); painlocation = txtitem.gettext().tostring(); //create intent intent intent = new intent (this, item1.class); //put desired extras intent intent.putextra("com.painlogger.painlevel", painlevelstring); intent.putextra("com.painlogger.paintime", timeofpainstring); intent.putextra("com.painlogger.treatment", texttreatmentstring); intent.putextra("pain location", painlocation); startactivity(intent); //set variables "", empty } }
my item1.class app goes when item on list clicked. has no other purpose.
i having problem because when add together item in list, replaces previous one, , left 1 item.
here tested prototype:
testresultactivity.java
package com.arrdude.forumanswer; import java.util.arraylist; import java.util.hashmap; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; public class testresultactivity extends activity implements onclicklistener { private static final int request_list_item = 235; //arbitrary request code static final string adapter_value_1 = "head"; static final string adapter_value_2 = "sub"; arraylist<hashmap<string, string>> masterlist = null; listadapter adapter = null; listview listview = null; button addbutton = null; public listadapter getadapter() { if(adapter == null){ adapter = new simpleadapter(this, getmylist(), r.layout.listitem, new string[] {adapter_value_1, adapter_value_2}, new int[] {r.id.listheading, r.id.listsubheading}); } homecoming adapter; } public listview getlistview() { if(listview==null){ listview = (listview) findviewbyid(r.id.mainlistview); listview.setadapter(getadapter()); } homecoming listview; } public button getaddbutton() { if(addbutton==null){ addbutton = (button) findviewbyid(r.id.mainaddbutton); addbutton.setclickable(true); addbutton.setonclicklistener(this); } homecoming addbutton; } public arraylist<hashmap<string, string>> getmylist() { if(masterlist==null){ masterlist = new arraylist<hashmap<string,string>>(); } homecoming masterlist; } public void addlistitem(string head, string sub){ hashmap<string, string> addme=new hashmap<string, string>(); addme.put(adapter_value_1, head); addme.put(adapter_value_2, sub); masterlist.add(addme); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //lazy init objects not efficient in android easier reading getlistview(); getaddbutton(); addlistitem("test head", "test sub heading here below"); //an initial item testing } @override public void onclick(view v) { intent launchadditemi = new intent(this, additemactivity.class); startactivityforresult(launchadditemi, request_list_item); } @override public void onactivityresult(int req, int res, intent data){ if(req == request_list_item && res == result_ok){ string thishead = data.getstringextra(adapter_value_1); string thissub = data.getstringextra(adapter_value_2); addlistitem(thishead, thissub); } } }
additemactivity.java
package com.arrdude.forumanswer; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; public class additemactivity extends activity implements onclicklistener { edittext headeredittext = null; edittext subheadedittext = null; button donebutton = null; button cancelbutton = null; public edittext getheaderedittext() { if(headeredittext==null){ headeredittext = (edittext) findviewbyid(r.id.headedittext); } homecoming headeredittext; } public edittext getsubheadedittext() { if(subheadedittext==null){ subheadedittext = (edittext) findviewbyid(r.id.subedittext); } homecoming subheadedittext; } public button getdonebutton() { if(donebutton==null){ donebutton = (button) findviewbyid(r.id.adddonebutton); donebutton.setclickable(true); donebutton.setonclicklistener(this); } homecoming donebutton; } public button getcancelbutton() { if(cancelbutton==null){ cancelbutton = (button) findviewbyid(r.id.addcancelbutton); cancelbutton.setonclicklistener(this); } homecoming cancelbutton; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.additem); //lazy init objects not efficient in android easier reading getheaderedittext(); getsubheadedittext(); getdonebutton(); getcancelbutton(); } @override public void onclick(view arg0) { switch (arg0.getid()) { case r.id.addcancelbutton: finish(); break; case r.id.adddonebutton: sendresult(); break; default: break; } } private void sendresult() { intent resulti = new intent(); resulti.putextra(testresultactivity.adapter_value_1, getheaderedittext().gettext().tostring()); resulti.putextra(testresultactivity.adapter_value_2, getsubheadedittext().gettext().tostring()); setresult(result_ok, resulti); finish(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/maintitle"/> <listview android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/mainlistview"></listview> <button android:id="@+id/mainaddbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/addbuttontext"></button> </linearlayout>
additem.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <linearlayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/linearlayout01"> <textview android:layout_width="wrap_content" android:text="header:" android:layout_height="wrap_content" android:id="@+id/textview01"></textview> <edittext android:layout_weight="1" android:layout_width="fill_parent" android:id="@+id/headedittext" android:layout_height="wrap_content"> <requestfocus></requestfocus> </edittext> </linearlayout> <linearlayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/linearlayout2"> <textview android:text="subhead:" android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content"></textview> <edittext android:layout_weight="1" android:layout_width="fill_parent" android:id="@+id/subedittext" android:layout_height="wrap_content"></edittext> </linearlayout> <linearlayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/linearlayout1"> <button android:text="cancel" android:id="@+id/addcancelbutton" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> <button android:text="add item" android:id="@+id/adddonebutton" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> </linearlayout> </linearlayout>
listitem.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:id="@+id/listheading" android:layout_width="wrap_content" android:layout_height="26dip" android:layout_alignparenttop="true" android:textsize="20sp" /> <textview android:id="@+id/listsubheading" android:layout_width="wrap_content" android:layout_height="22dip" android:layout_below="@id/listheading" android:textsize="16sp" /> </relativelayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="maintitle">list items:</string> <string name="app_name">testresultactivity</string> <string name="addbuttontext">add new item</string> </resources>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.arrdude.forumanswer" android:versioncode="1" android:versionname="1.0"> <uses-sdk android:minsdkversion="4" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".testresultactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".additemactivity"></activity> </application> </manifest>
happy coding!
java android listview android-intent
Comments
Post a Comment