Dinesh
Dinesh

Reputation: 181

Adding an item to a listview in a different activity from the current activity on click of a button

I want to add an item to a listview that is present in a different activity from the current activity. Say I have a button in my current activity and when this button is clicked I want to add an item to the listview present in a different activity. Can anybody suggest me a work around for this? Thank you.

Upvotes: 0

Views: 2908

Answers (3)

user1203673
user1203673

Reputation: 1015

When the button is clicked store the data in shared preference and when u are starting the list activity get the data from shared preference and populate the list delete the data from the shared preference

Upvotes: 0

Anordil
Anordil

Reputation: 150

You can use Intents.

You have to launch your 2nd Activity with a startActivityForResult (Intent intent, int requestCode). Then before terminating it you must put the data that you wish to add to the 1st Activity in an Intent.

Finally, in your first Activity, you get this data in the onActivityResult() method.

Here's a detailled example: http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html

Upvotes: 1

Selvin
Selvin

Reputation: 6797

you should use startActivityForResult(for start SelectorActivity) in current activity(lets call it ListActivity) then in second activity (lets call it SelectorActivity) you have to put some confirm button(OK) and when you click OK button you should call setResult and finish SelectorActivity

back in ListActivity you should override onActivityResult and add data from SelectorActivity to list Adapter

first look here http://developer.android.com/reference/android/app/Activity.html#StartingActivities

Edit: you should return data in Intent ... use Intent.putExtra() in setResult and then Intent.getExtra() in onActivityResult

Upvotes: 0

Related Questions