I'm trying to create my first android app and now I've got into a design problem. I'm making a jokes app(all the jokes are to be stored offline) and i'm not sure how to deal with putting the jokes and then retrieving them. I found out about TypedArrays, which would seem like a good idea, but my Joke class contains enums, and I'm not sure if I can set enums from the TypedArray.
jokes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="texts">
<item>Sure, white people can't say the "n word" but, atleast we can say phrases like,"Thanks for the warning, Officer" and, "Hey, Dad."</item>
<item>I cheated on my girlfriend once. I was playing monopoly and I took some money from the bank when she wasn't looking.
Then I went upstairs and fucked her sister.</item>
<item>'Yo Momma' jokes are old and worn out. Just like… Yo Momma.</item>
</array>
<array name="category">
<item>OFFENSIVE</item>
<item>ANTIJOKES</item>
<item>YO_MOMMA</item>
</array>
<array name="length">
<item>SHORT</item>
<item>MEDIUM</item>
<item>SHORT</item>
</array>
</resources>
Joke.java
//TODO add more joke categories
private enum CATEGORY{OFFENSIVE,YO_MAMMA,ANTIJOKES}
public enum LENGTH{SHORT,MEDIUM,LONG}
private String mText;
//I'm not sure about this one, is just a placeholder for the audio file of the joke
private File mAudioFile;
private CATEGORY mCategory;
private LENGTH mLength;
public Joke(String text,CATEGORY category,LENGTH length){
mText = text;
mCategory = category;
mLength = length;
}
MainActivity.java
public Joke getJoke(int index){
Resources resources = getResources();
TypedArray text = resources.obtainTypedArray(R.array.texts);
TypedArray category = resources.obtainTypedArray(R.array.category);
TypedArray length = resources.obtainTypedArray(R.array.length);
Joke joke = new Joke(text,category,length);
}
I'm open to suggestions to do this in another way.
I would suggest to use JSON. You can store your jokes as JSON in files and use Gson to unmarshal the Jokes into POJOs. Also you should change File mAudioFile
to String mAudioFile
and detect audio file by name to avoid problems with JSON marshaling/unmarshaling.
Update Your POJO can be looks like this:
public class Joke {
private String mText;
private String mAudio;
private CATEGORY category;
private LENGTH length;
public Joke(String mText, String mAudio, CATEGORY category, LENGTH length) {
this.mText = mText;
this.mAudio = mAudio;
this.category = category;
this.length = length;
}
public String getmText() {
return mText;
}
public void setmText(String mText) {
this.mText = mText;
}
public String getmAudio() {
return mAudio;
}
public void setmAudio(String mAudio) {
this.mAudio = mAudio;
}
public CATEGORY getCategory() {
return category;
}
public void setCategory(CATEGORY category) {
this.category = category;
}
public LENGTH getLength() {
return length;
}
public void setLength(LENGTH length) {
this.length = length;
}
}
And your JSON sample is:
{
"mText": "text",
"mAudio": "audio file path",
"category": "YO_MAMMA",
"length": "LONG"
}