I'm trying to save a stack of objects, so that the stack will still be there when I close and then reopen the app.
I tried extending Application to create a global variable, but that is deleted when the app closes. I think I should be using Java's internal memory, but don't know how to do so with a stack and not a single object. I am having trouble researching this, as whenever I look for anything with "stack" in the search, everything is about the runtime stack, not the data structure.
Best way to store large data objects is internal SQLite database with help of Room, OrmLiite or other wrapper.
Or if it small values like saved options, time or similar then use SharedPreferences storage
The object needs to be serialisable. For this you need to extend your object with Serializable
https://developer.android.com/reference/java/io/Serializable
You need to store the object to the disk in the form of a file or in a database as a blob.
When you start your application you need to deserialize the object and load it into memory.
There is an alternative to the above 3 points though.
There is an open source library called SnappyDb which can persist key value pairs and do serialization and drserialization pretty fast.
https://github.com/nhachicha/SnappyDB
you need to use sharedPreferences.
i will show you how to save an string and use it .
step1 : SharedPreferences sharedPreferences;
step2 : sharedPreferences= this.getSharedPreferences("ur choosen name",0);
step3 : sharedPreferences.getString("mystring","");
step4 :`sharedPreferences.edit.PutString("mystring","what i want
save").commit();`
now its saved
step5 : String mystring;
step6 : mystring = sharedPreferences.getString("mystring","");
use step 5 and 6 for using your saved variable
Related
In terms of context for what I'm doing, I have an ArrayList with some String[]-typed contacts in it (has a {name, phone number, phone type} per entry) for the native android address book feature. I want to store this list of contacts, and then bring them back up whenever a new list of contacts is added so that I can compare the two for differences. However, I'm not entirely sure what I could do to store one for later use. I was going to use SharedPreferences, but at the most it only takes a Set, not an ArrayList; plus, since it's contacts, a Set may prove to be problematic in the event of any duplicate values.
The other option I considered was some sort of conversion into another type that SharedPreferences accepts, then converting it back after retrieving it. I had to put a pin in that as well because a lot of those methods require external libraries that I cannot add to this project (for dependencies).
Are there any native methods I could use in order to properly store an ArrayList<String[]> for later comparisons?
The proper way would be to use a contact class combined with sqlite table.
However, you could also create an xml/text file to store your arraylist data and read it back when needed.
I am trying to get value like key and value pair but i am doing it from json file and now there is another approach suggested lets do it from db tables. because if in future value change then only update the DB is Needed.
I think using json file is more good as value hardly going to change in future(rarest of rare).. although advantage of db approach is just change the db value and done...
So My point is json will be faster then DB and Using Json will reduce load on DB..as clicking UI it invoke extra call of DB..
What do you Think .. Please let me know..
This very much depends on how you are going to use these data.
Do you need to update it often?
Do you need to update by just one specific field?
Do you need to fetch records based on some specific field?
Do you need to fetch whole json or just some specific fields?
Do some parts of json reference any other tables?
Also, consider the size of those data, e.g. if the json files together may become more in size than the whole other tables, you may break db cache. From the other hand, you can always create separate database for your json files if you still need some relational database features.
So, I would anyway start with answering first 5 questions.
I'm making a game of Tetris on android as a project for school and right now im using shared preferences in order to save the current state of the game so that it can be resumed on a later time , i've come to realize that when you store over 100 or so preferences the sharedprefernces object starts working in a strange way , i can save everything but when i try to call the editor to clear (e.clear + e.commit) it wont remove the preferences.
i would appreciate any help regarding this issue
thanks
SharedPreferences are good and lightweight mechanism how to persist data.
But i think for game it's not a win at all. SharedPreferences are usually used for persisting non-structured data for example if you have some application that requires login and when User is logged in successfully you can save this state to SharedPreferences and in next Activities just check it whether User is logged in or not. But in the game you have (i guess for sure) structured data-structures (for instance players and their properties (values) like reached score, loses, wins etc.).
So i suggest you to think about another mechanism for data persisting. Specifically try to think about a possibility to use classic object serializing or and usage of SQLiteDatabase which provide more complex solution how to persist structured data.
A main advantage is that you can persist (serialize) whole objects and then simply deserialize them (and not persist them as specific "chunks" in SharedPreferences). Regarding to SQLite, it provides almost same solution as classic serializing but objects are represented as tables in database.
If you need to remove specific values use this:
SharedPreferences.Editor.remove() followed by commit()
To remove them all SharedPreferences.Editor.clear() followed by a commit()
(references here https://stackoverflow.com/a/3687333/1584654).
However If the values remain limitated, for Shared Preferences should not be an issue.
I am storing some data in a hash map. Now I want to modify the values associated with a key based on a user input and store them these way permanently.
To make myself more clear, I have a hashmap like this:
public static HashMap<String,Integer> mymap= new HashMap<String,Integer>();
mymap.put("Hi",2);
mymap.put("Hello",3);
I will take feedback from user in some user and if he wants then I will, say, store 4 against Hello. I want these changes to be saved for future references.
I have heard about Reflection API in Java, but am not sure whether that will serve the purpose.
Reflection API allows one to manipulate/access data that is not accessable otherwise - or some data on the class that is unknown at compile time.
In here, it is really not needed. All you need is to put() the element into the map, it will "remove" the old value from the key you just inserted (if it is already there) and associate it (the key) with the newly added value.
So, basically - all you need to do is myMap.put(key,newValue), and the implementation of the Map (assuming it is a correct one, of course) will take care of the rest.
If you want to store the data between runs of the program - you will have to save it (the map) on disk. In order to do so, you can use serialization, or if you can use Properties in some cases.
Make sure that you load the map from disk once the program starts, or you will not see the values you stored.
Just say, mymap.put(key,value);. It will update the value for matching key. If not there, it will insert a new entry e.g.
mymap.put("Hello",4);
If you don't want to insert new value for a new key e.g. World, you can put a check like this:
if(mymap.containsKey(key)){
mymap.put(key,value);
}else{
//no existing key found
}
The Preferences API makes it easy to store a small amount of data on disk. It's usually used to store configuration data. It's similar to the Windows registry.
Here's an introduction: http://docs.oracle.com/javase/1.4.2/docs/guide/lang/preferences.html
My app uses a SQLite database for the information. I have a function that checks to see if the folder and database are already present, if they aren't it will go on the internet ( currently I am using dropbox to store the db file ) and download the database and store it on the sd card, then I it will open the database. The database is writable as it lets the user rate an object. I have two questions.
1.) I would love to provide updates to the database and then have my app update the database if the version number is higher and replace the existing one. I have done some research and from what I have found it is possible to store an xml or json file with the version number of and the just parse the information and if the version number is higher download the new database.
Can someone provide an example of how this is accomplished and whether it is better to use xml or json for this task?
2.) Is there a way to save the rating in the new version of the database when the new is downloaded and accessed?
Thanks
two nights ago I wrote something like that.
pack your database structure as an array in a webservice method by reading field names and field types. the structure of array is arbitrary.
call web service method and you must receive a string that represent a JSONArray object, if you sent it as json with json_encode() method in php.
read structure and make CREATE DB query string with for loops.
execute query, so you must have database.
also you can send alot of information with arrays.
introducing each part is hard, so for each part google it.
don't forget to convert field types to match SQLite types such as VARCHAR=>TEXT, smallint=>INTEGER , ...