Question

I'm working on a android app (to work with old androids as well as new, so no 3.0 tricks), and I'd like to store submit data in an arrayList in my app, that can be shared across views. The data is first stored instantiated into an object, then it will be an arrayList. I'd like to have access across views, (sharedPref maybe?) but I also need to make sure if the app crashes the rest of the data is not erased.

Any suggestions?

Was it helpful?

Solution

Here's a really good tutorial for getting an SQLite database setup and understanding how it works, which also includes a pretty good helper class you can modify to fit your needs:

http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android

In my comment, I was trying to explain that if you have an ArrayList of Objects, you can store each object in the database, and then re-populate your ArrayList using that.

IE. You have a Objects A and B with an int ID and a String name. Store each object's ID, name, etc., in the database. When you go to pull them back out, re-create your object (IE. A/B) from the values stored (IE. ID/name), and then add them to an ArrayList containing all your Objects of that type.

Hopefully I explained that well enough, but either way, that link I provided will definitely point you in the right direction

OTHER TIPS

If you are simply trying to share across Views in the same activity, you would just reference the Resource ID via R.id.yourResourceID when you set the view. However, if your views are in different Activities, you need something a little more robust.

To pass an object across Activities, you need to extend the Parcelable interface within your Object. This will allow you send the object to each of your activities, and retrieve the data from within the object.

This might help with passing objects across activities: http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top