I need to save List of simple objects
class simple{
String name;
byte[] data;
}
in android. How to do this ? Do I need SQLite or there is easier way ?
Just refer serialization in java, and that makes your objects to be serialized.
see an example here,
http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29
I'd use a database for that. It would make easier to get that data from different activities. Here is a great tutorial on dealing with database in Android.
I assume you can take that byte array as an INTEGER one for database type compatibility (or maybe some clever conversion to something else). The tutorial above provides you with info on the data types.
Related
I'm really new to databases, but I'm making a running tracker app in Android, and I have some classes that represent the type of goals that you can have on a single type of workout.
What every Goal class does actually is to store a value but together with a unit, like for example a DistanceGoal will represent its value in meters and provide methods to convert it to kilometers or miles. A PaceGoal value will represent minutes per kilometers, and so on.
Also, according to type of Goal I will then have to read all tracking stats to see if the user has achieved the objective or not.
So:
interface GoalĀ {
long getGoalValue();
}
class TimeGoal implements Goal{}
class DistanceGoal implements Goal{}
class PaceGoal implements Goal{}
/* ...... */
Now this works well, although there may be a better way to solve this. The problem is that I need to store this in a database.
I have been using the Android room library, and I couldn't find any other way than to store each type of goal in its own table, so when I need to retrieve them I know what stored value represents what kind of goal.
But when I have to store a new goal, it will probably be given as its abstraction (the Goal interface), so I don't know in which table to store it.
The only solution I thought of is the Visitor Pattern, do any of you have a better solution for this?
I may have to make changes to the whole class hierarchy, but what really matters is to distinguish the unit from one value from the others, in order to read the tracking statistics accordingly.
You could make the Goal classes serializable. Then you can serialize the Goal objects to a byte array that you can store in your database. When you retrieve the byte array from the database, you can deserialize it to get the Goal object back. When you Google serializable java you should get lots of information on how to do this.
If I have a class containing of integers, strings and a Date, how should I go about creating, saving and displaying a new instance of this class in my ListViews?
I've used SharedPreferences before for saving local strings but is this possible with objects as well, or should I look into SQLlite?
Edit: Also, if SQLlite is needed, a little steer in the right direction would be appreciated.
You can still use preferences to save your objects but you need to serialize them into strings. And for later use you have to deserialize them from strings into your list of objects. This can easily be done with Json.net but I would recommend to use a database like SQLite. This would make it easy if new requirements like searching or object extension comes up.
A good entry into SQLite and Xamarin can be found here or here.
I have implemented a standard LRUCache in Android that stores Objects. Each key is a unique ObjectId associated with the Object stored. My problem is that the only way to retrieve an Object from cache is by the ObjectId (no iterator). What would be the best way to implement a getAll() method?
Another option would be to store all the ObjectIds in a list somewhere, so I can iterate over the lists and get all of the Objects - but what would be the best way of holding all of the ObjectIds?
Thanks!
If you're using (or extending) the LruCache that Android provides, it has a snapshot method that returns a map of keys (your ObjectIds) and values (your Objects). You can do something like this:
Map<ObjectIds, Object> snapshot = lruCache.snapshot();
for (ObjectIds id : snapshot.keySet()) {
Object myObject = lruCache.get(id);
}
If you're not using Android's LruCache, then I imagine it would depend on your implementation. (I'd also be curious what motivated you to implement your own instead of subclassing the provided one!)
Using snapshot to get current collection at the moment
lruCache.snapshot().values()
It does not make sense to iterate over the objects in a LRU cache. You can not know which object is still in the cache and which got evicted (you actually can, but that's another story). It sound like you'd probably better off with a different data structure like a Hashmap or so. Nothing will ever get evicted from there.
A common use-case is to have a List of all possible object keys in memory. If you need one, you check if it is in the cache. If not, receive it and add it to the cache.
I am porting an app from Android Java to iPhone.
In Android I used Lists/ArrayLists alot.
On iPhone I plan to use NSMutableArray.
Is there any way to define or even indicate the type of objects in an NSMutableArray.
I know one can put any type of object there, but I would like to make it more visible and transparent.
Many thanks
It's not clear exactly what you're asking.
If you just want to make it clear to the reader what sorts of object of are in the array, just name it appropriately (you can't enforce it at the language level):
NSMutableArray *arrayOfMyClasses;
If, on the other hand, you want to find out the type of an object that you're reading back from the array then you can get the underlying class using:
[obj class]
Or easily compare to other class types:
if ([obj isKindOfClass:[MyClass class]) { ... }
Tim
I assume you are looking for template pattern in Objective C. Unfortunately, it is not available in Objective C (at least directly).
You might find this question of StackOverflow.com interesting
You can only indicate a type.
for(id obj in _assets) {
NSString *className = NSStringFromClass([obj class]);
NSLog(#"%#", className);
}
Arrays are ordered collections of any sort of object. For example, the
objects contained by the array in Figure 1 can be any combination of
cat and dog objects, and if the array is mutable you can add more dog
objects. The collection does not have to be homogeneous.
Collections Programming Topics - Arrays: Ordered Collections
i have a basic function which requires serializing in my android app. The user will add some values to an ArrayList and i want to serialize it to avoid using a database for this little option and of course TO LEARN how to serialize (i'm a begginer) because it seems useful. Anyways the user save something in the arraylist, the program shuts down, the program starts up again and the user is able to see the saved data. How can i implement this? Can you provide some code snippet or a useful link?
Thanks a lot!!
You can do this by custom bean class and implement Serializable to that
so now when you create ArrayList<E> of that class it is Serializable.
Example:
Class dataBean implements Serializable
{
public String name;
}
ArrayList<dataBean> dataBeanArrayList = new ArrayList();
So dataBeanArrayList is now Serializable and you can also pass this between Intent.
I suggest using flexjson to serialize the data to a file. Then you can read that back using that library. This has several advantages over serialization which is being able to load your stream back into potential differing versions of your objects. Using ObjectInputStream you have to be very careful, and quite frankly I've never seen it work all that well.
http://flexjson.sourceforge.net
Here is a blog post how to do that:
http://wrongnotes.blogspot.com/2010/09/flexjson-meet-android.html