I want to store my HashMap inside my application so everytime i restart the application i can retrieve my HashMap. I've seen many ways via Google but what is the most efficient way to do it in my case?
You should use the preferences if the amount of data is not to big. Convert your HashMap to JSON (maybe via gson) and than store it as a string.
If you have a lot of data you need to access fast you can use the integrated sqlite database.
If you are using the HashMap as a means of retrieving constant data (i.e. the values inside the HashMap won't change), it is as simple as initializing the HashMap in your code as follows:
private static final Map map = new HashMap();
static {
map.put(...);
map.put(...);
/* etc... */
}
If you want this information to be globally accessible, you can create a subclass of Application and initialize it there instead.
Related
How can I to put data to spinner which is on another layout?
I am newby on android, and now I have problem with spinners.
I have data at first class with his layout. And I want to put that data to other spinner which is on another layout. What I need to do? Create new Class or What?
Please help! :)
Data is- Array
You should put your data where it is accessible from everywhere.
so please put your data in database using SQLite or ORMs that help you work with Native database like SugarORM, ORMLite and Realm.
another way is to apply singleton pattern to store your data in a different class.
You said you have an Array
Then make Array static
eg:- public static ArrayList<> new_array = new ArrayList<>;
now call this array any where in the activity
you will get data
there is many to manage array data.
- create arraylist and use that data anywhere in the activity.
- using database also you can user.
- using Read & writing arrays of Parcelable objects
- using global application class ....etc
I'm new in programming for Android so maybe my question will be very easy to solve but still. I'm trying to make an array of different data types :
I have to add there :
int number
String name
int number_2
int time
int total
And my question now is how to implement it in easiest way, and how to get data from it. In case that I have to get a different records for this variables and store it into list .
Also have a question about way how to keep all values which I handle inside of my array.
I have to keep it because in my program I have to go back to other activities go forward to another and again collect data and add it to my array.
What will be the best and easiest solution ?
Thanks in advance for help
You could create the Array as an Array of Objects. All other classes are derived from Object, so you'll be able to store all types of objects in your Array. However, you would have to check the type of an object you get from the Array, before you'd be able to safely interpret as an object of a specific class. Moreover, you would have to use Integer instead of int.
If all (or at least multiple) of your elements you are intending to store in the Array are belonging to one (physical) entity, you could create a custom Class that holds its own properties as class members, and fill your Array with a list of instances of this Class.
Moreover, if you plan to add elements to your Array, you should use a List instead, e.g. an ArrayList.
As for retaining your data, you would have to either store it in a database, or save it to a file. In either way, you will have to save it upon close of the Activity, and load it again once the Activity starts
To pass the data across activities you will need to pass them using objects you can store in an intent. Seems like the best way to handle that is to either create a PREFS file to store the data or to create an object that extends Parcelable like here:
https://stackoverflow.com/questions/18593619/android-parcelable-object-passing-to-another-activity
Parcelables are preferable assuming you need all the data in a single object, you do not want to "putExtra" a bunch of fields and you also want to be sure data can pass from one activity to another. Otherwise, a simple Util class that reads and writes to a PREFS file is the way to go:
android read/write user preferences
A database is always another option, but seems well outside the scope of your question.
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 have an Activity which first needs to determine the output based on input, with a help of HashMap. I am storing 25 pairs in it.
The problem is that HashMap initializes every time the activity starts. That got me thinking about the best way to store these pairs - should they be in an "external resource", or maybe some other solution - to make pairs persistent?
Putting the data from your hashmap into some persistent storage will probably not help you to get around initializing the hashmap when creating your activity. If your application frequently uses this activity it might be more worthwhile storing the Hashmap as a field in the Application class and then accessing from any activity via the application context.
SampleApplication extends Application{
private HashMap<String,String> mMap = null;
public void onCreate(){
//initialize your hashmap here
...
}
public getHashMap{
return this.mMap;
}
}
To get the map from any activity in your project just use the following code:
HashMap<String,String> map = ((SampleApplication)getApplicationContext()).getHashMap();
If you still need to store the data persistently you can choose one of five methods listed here. HashMap does implement Serializable so I would probably use SharedPreferences if it were me.
EDIT Mert's answer is great if the string's you're using never change, just use string resources instead. But if you need to do a fair amount of manipulation the convenience of a collection might still make the approach in my answer worth it.
It depends how use your Strings..
You can store them on String XML if all fixed and perminent.
You can make a class and store in ApplicationContext if you use only application runs.
You can keep in file/sqlite db or user settings.
I am developing an Android application. The content for the application is taken from the webserver using webservice. I am taking the data from webserver and store in mobile db and accessing across the application and its works fine but now problem is while taking more records from mobile db it tooks some time and it application hangs.
Is it possible to maintain the java collection such as hashmap or hashtable to maintain throughout the application until user logout the application and user can add or update the data in collection. If so please guide me.
you should use Application class that ships with Android API .
See this link..
http://www.xoriant.com/blog/mobile-application-development/android-application-class.html
First of all it's better to user db for storing purpose. you should look for optimizing you db querys and the they way you are using it. But anyways you can create a static hashmap or arraylist of your object in your first activity. Then you can use this collection object anywhere you wanted and perform any opration, it will be intect untill and unless you logout of application or any exception occures while working on the collection.
class MylunchedActivity extends Activty{
public static HashMap map = new HashMap();
}
MylunchedActivity should be your first activity. Then by using MylunchedActivity.map, you can work on you map.
i hope this will help.
Regards,
Ravi
use a standalone class or static class to maintain run time static data you need to store.
Created a global singleton in the Application class and was able to access it from the activities I used.
You can pass data around in a Global Singleton if it is going to be used a lot.
public class YourApplication extends Application {
public SomeDataClass data = new SomeDataClass();
// Your JAVA collections...
}
Then call it in any activity by:
YourApplication appState = ((YourApplication)this.getApplication());
appState.data.UseAGetterOrSetterHere(); // Do whatever you need to with the data here.
For more info look at Android Application Class
Here are few possible solutions :
How to store hashmap so that it can be retained it value after a device reboot?
Saving a hash map into Shared Preferences
One is serialize/deserialize and other is using sharedpreference, as your requirement is to save/edit data until user logout.