-1

i am struggling to keep data or items of list view when i leave one activity to another, for my example i created simple app on click will add number increased to a list view so each click create an item like 1 another click add 2 and so on.

the program works fin for main activity but then i would like to see the result on another activity i call it second activity but the problem is when i hit back button on second activity to go back to main activity, i will lose all of items on the list view.

i was looking on google so many information but could not get my head around it, so please advice and please be little more in detail as i am beginner, i think instance state or shared preference will do the job but i do not know any of them

thanks in advance and here is my app code for main activity and second activity and picture for output sorry i add code as images becausethe site keep saying the code need indentation thank you main activity[main out put][2]second activity[second activity out put][4]

SeekingKnowleges
  • 123
  • 2
  • 10

2 Answers2

0

You need to save the data of the ListView in some form, either in a file or in a database (local or remote).There is no direct way to store list view, but you can store the data from the list view and then set it in to the ListView later when you switch back to the activity.

You need to keep in mind that switching activity results to invoking of onPause() method in android, and if the data is not saved in the current activity then it will be lost when you move on to another activity.

Somdip Dey
  • 3,346
  • 6
  • 28
  • 60
  • thanks for quick reply, what i need is to create temporary list view so on click button add item and on click button remove that button, because my data is not fixed, so either i can add or delete , therfore i think it is hard to save it in file , but i do not know if i save them in database then if i click on individual item on listview i be able to remove individual item on list view or not? – SeekingKnowleges Jun 02 '17 at 16:13
  • What you are looking for is: Saving Data in Shared Preferences (google about it). You can also add or remove items to the list view based on the saved data but the implementation would depend on you based on your exact requirements. – Somdip Dey Jun 02 '17 at 16:39
  • One advice, Shared Preferences only allow saving data in Key:Value pair, so use it wisely. – Somdip Dey Jun 02 '17 at 16:42
  • And to be able to delete items from list view by clicking on it, you need to set an onClickListner on the list view and delete the item based on its position, which is clicked. If you google each of these features separately, you will find many examples. – Somdip Dey Jun 02 '17 at 16:49
  • Somdip De thank you but i do not know how to use shared preference at all , no easy example on google all similar and does different tasks. – SeekingKnowleges Jun 03 '17 at 05:03
0

Add all your values into the array, pass it to the adapter, then after clicking on the list view item, make an intent where you want to switch your activity (means from one activity to second activity), while implemented the intent, pass your array also, using intent.put extra. Then you will get your array in your second activity and coming back to your previous activity, again pass an intent (with your array) and get your array in your previous activity and pass it into the adapter.