Add item to listView after passing info through an intent Android?

Here is a 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> 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}); } return adapter; } public ListView getListview() { if(listview==null){ listview = (ListView) findViewById(URL1. Mainlistview); listview. SetAdapter(getAdapter()); } return listview; } public Button getAddbutton() { if(addbutton==null){ addbutton = (Button) findViewById(URL1. Mainaddbutton); addbutton.

SetClickable(true); addbutton. SetOnClickListener(this); } return addbutton; } public ArrayList> getMylist() { if(masterlist==null){ masterlist = new ArrayList>(); } return masterlist; } public void addListItem(String head, String sub){ HashMap addme=new HashMap(); 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 the most efficient in Android but easier reading getListview(); getAddbutton(); addListItem("Test Head", "Test sub heading here below"); //an initial item for 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); } return headeredittext; } public EditText getSubheadedittext() { if(subheadedittext==null){ subheadedittext = (EditText) findViewById(R.id. Subedittext); } return subheadedittext; } public Button getDonebutton() { if(donebutton==null){ donebutton = (Button) findViewById(R.id.

Adddonebutton); donebutton. SetClickable(true); donebutton. SetOnClickListener(this); } return donebutton; } public Button getCancelbutton() { if(cancelbutton==null){ cancelbutton = (Button) findViewById(R.id.

Addcancelbutton); cancelbutton. SetOnClickListener(this); } return cancelbutton; } @Override public void onCreate(Bundle savedInstanceState) { super. OnCreate(savedInstanceState); setContentView(R.layout.

Additem); //lazy init objects not the most efficient in Android but 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"?

> List Items: TestResultActivity Add New Item.

Here is a 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> 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}); } return adapter; } public ListView getListview() { if(listview==null){ listview = (ListView) findViewById(URL1. Mainlistview); listview. SetAdapter(getAdapter()); } return listview; } public Button getAddbutton() { if(addbutton==null){ addbutton = (Button) findViewById(URL1. Mainaddbutton); addbutton.

SetClickable(true); addbutton. SetOnClickListener(this); } return addbutton; } public ArrayList> getMylist() { if(masterlist==null){ masterlist = new ArrayList>(); } return masterlist; } public void addListItem(String head, String sub){ HashMap addme=new HashMap(); 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 the most efficient in Android but easier reading getListview(); getAddbutton(); addListItem("Test Head", "Test sub heading here below"); //an initial item for 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); } return headeredittext; } public EditText getSubheadedittext() { if(subheadedittext==null){ subheadedittext = (EditText) findViewById(R.id. Subedittext); } return subheadedittext; } public Button getDonebutton() { if(donebutton==null){ donebutton = (Button) findViewById(R.id.

Adddonebutton); donebutton. SetClickable(true); donebutton. SetOnClickListener(this); } return donebutton; } public Button getCancelbutton() { if(cancelbutton==null){ cancelbutton = (Button) findViewById(R.id.

Addcancelbutton); cancelbutton. SetOnClickListener(this); } return cancelbutton; } @Override public void onCreate(Bundle savedInstanceState) { super. OnCreate(savedInstanceState); setContentView(R.layout.

Additem); //lazy init objects not the most efficient in Android but 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 additem. Xml listitem.

Xml strings. Xml List Items: TestResultActivity Add New Item AndroidManifest. Xml Happy coding!

Thank you so much. I'll try it quickly and get back to you. – Kgrover Jul 16 at 1:36 when I say startActivityForResult(intent, int), it needs an int...what would that be?1?

That is the int you have placed before finishing... – Kgrover Jul 16 at 1:39 does this way allow me to retrieve and add extras the way I am doing so? After doing this, my list-item is not showing up in my original activity. – Kgrover Jul 16 at 1:55 1 The int you asked about is simply a code that you provide to know where the intent came from.It can be useful if you want to process things differently depending on what state something was in when it sent the request.

SetResult() also requires an int as a response code. This is useful for the same reasons. I use it for returning error conditions sometimes as an example.

What ever the int that goes in there, only your code needs to know how to interpret it. – mbarnes Jul 16 at 1:57 It definitely lets you do the extras pass-through you are doing. I would say that it is the reason for the intent and result design.

I did not really check my code so play with it a little I may have messed up your logic with cut and paste. – mbarnes Jul 16 at 1:59.

First, Hashmap is suppose to be a collection, it seems you need only a pair of object, which, Map. Entry is far better solution (or even better, use a custom inner class). Second, you add the item each time the addScreen Activity is created, and each time it is created, your painItems are freshly created.

That's the reason you see only first content there, you need to have some way to persist your list (at least within application life cycle). Quick fix (though may not be desirable) is to put static: private static List> painItems = .... should do the trick.

Like for(i=0, i> painItems = new ArrayList>();, as in change the label to private static – Kgrover Jul 16 at 0:59 static will live as long as your application stays in memory, and share across different instance of your activity class. – xandy Jul 16 at 15:26 1 startActivityForResult() is a bit similar to dialog system in Windows, your parent call startactivityforresult, and wait for the child to comeback (which, your child should call setResult()) to return the result back to the parent. Check your code through those... I think you can try logging (the intents) of onCreate, onResume, onPause and onDestroy etc. In your parent and child activities to get better understand the calling stack of Android.

– xandy Jul 16 at 16:51.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions