last day (57 days later) » 

8:32 PM
0
A: How to save data and re-open the last used Activity

F43nd1r Why does it happen? The Bundle created in onSaveInstanceState is only kept for short-term restarts, e.g. when your activity is recreated due to an orientation change. If your activity stops normally this Bundle is not kept, and thus can't be delivered to onCreate. What can I do to fix th...

 
FET
Should I check if my SharedPreferences is null in order to run the code as the first time?
Thus, how do I pass an ArrayList of custom Object (Player in my case) though SharedPreferences?
 
Yes, you have to check if the values exist before using them. the SharedPreferences itself will never be null. To save custom objects you have to serialize them in one way ore the other. If they are POJOs, I'd recommend GSON.
 
FET
Ehm.. may I ask you a little snippet to see how to implement all this? I mean, how I should check at the beginning the null before reading the new values, and how to save the custom objects. That would be the bounty, thanks!
 
See my edit....
 
FET
Is this the Gson you're using? github.com/google/gson
 
8:32 PM
Yes, I am using Google's Gson v2.7, which should be one of the recent versions.
 
FET
Hey, what if I have a List of custom Objects (List<Player) and I want to save and then read it back? Because now I get errors in some loops in the code where I loop inside this list and it tells me I should write Object instead of Player
 
The best way to save lists with Gson is to convert them to arrays before saving and convert them back afterwards.
 
FET
Alright but, think like you've an ArrayList<CustomObject>, and you need to retrieve that, how you do that?
 
61
Q: How to deserialize a list using GSON or another JSON library in Java?

Valter HenriqueI can serialize a List in my servlet on GAE, but I can't deserialize it. What am I doing wrong? This is my class Video in GAE, which is serialized: package legiontube; import java.util.Date; import javax.jdo.annotations.IdGeneratorStrategy; import javax.jdo.annotations.IdentityType; import ...

 
FET
Checking
Oh hey, but what is actually the first argument that Gson takes in?
 
FET
Alright, I'll let you know if it all works
 
FET
8:57 PM
Hey
When in code, I have loops like the following
I get this error
for(Player player : players){
if(!player.getCardName().equals("Wolf")){
villagersLeft++;
}
Error:(286, 45) error: incompatible types: Object cannot be converted to Player
Any ideas?
This is my else statement which should read back the values:
else {
// Restore value of members from saved state
village = prefs.getString(VILLAGE, "");
// Lists

players = new Gson().fromJson(PLAYERS, new TypeToken<List<Player>>(){}.getType());
recentlyKilled = new Gson().fromJson(RECENTLY_KILLED, new TypeToken<List<Player>>(){}.getType());
playersCanVote = new Gson().fromJson(PLAYERS_CAN_VOTE, new TypeToken<List<Player>>(){}.getType());
playersNoWolves = new Gson().fromJson(PLAYERS_NO_WOLVES, new TypeToken<List<Player>>(){}.getType());
highest = new Gson().fromJson(HIGHEST, new TypeToken<List<Player>>(){}.getType());
 
9:20 PM
where does PLAYERS come from? have you checked that it is not null?
and not empty?
 

  last day (57 days later) »