6

Possible Duplicate:
How to pass a value from one Activity to another in Android?

I have an activity with a list of titles and their bodies(content) (list6 example from ApiDemos). And I have an activity, where I add a note. When I click on "Add" button, I want the title of my note to appear on the list. Same with body. The problem is, that in List activity there are two String[] arrays with predefined hard-coded titles and bodies. What should I do to be able to add my own new titles with a content instead of having these hard-coded values? I know I must use intents with startActivityForResult, but that's probably all I know...

Community
  • 1
  • 1
lomza
  • 9,412
  • 15
  • 70
  • 85

2 Answers2

31

You have two option:

1) make the listview and the two arraylists static

This way you can use the same instances of it from the activity with add button and modify the listview as:

FirstActivity.listTitlesArrayList.add(listTitleString);
FirstActivity.listDescriptionArraylist.add(listDescriptionString);//this is probably your note
FirstActivity.listView.invalidateViews();

2)if you want to use intents:

while going to the ListActivity pass data by..

intent.putExtra("Title", listTitleString);
intent.putExtra("Content", listDescriptionString);
startActivity(intent);

and to recover it in second activity use:

title= getIntent().getExtras().getString("Title");

...and so on..

GAMA
  • 5,958
  • 14
  • 79
  • 126
Vicky Kapadia
  • 6,025
  • 2
  • 24
  • 30
  • Thank you. I gonna use the second option. – lomza Mar 31 '11 at 09:46
  • The program runs, but there are several problems... In the second activity instead of string values I get sth totally different - "android.widget.EditText@4051fa30" as a title and "android.widget.EditText@405208c0" as a body. Also, I want to add titles and bodies dynamically to my arrays...In the first activity I have: http://codeviewer.org/view/code:18fb and in the second: http://codeviewer.org/view/code:18fc – lomza Mar 31 '11 at 10:51
  • Please, write me on tonia.tkachuk@gmail.com as this topis is closed already =( – lomza Mar 31 '11 at 11:24
  • hey you are wrongly getting the text from editbox. This is how you do it.: intent.putExtra("Title", titleEdit.getText().toString()); – Vicky Kapadia Mar 31 '11 at 12:00
1

There are several ways to share data between activities.

In your case probably the easiest way is to have a reference to list in Application. This answer summs it up nicelly: Making data obtained in one activity available to all the activities

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154