Reputation: 3
I am currently working on my first app, its a quiz app and I am trying to implement a statistics option to show % of correct/incorrect questions.
To do this, every questions has a field "isAnswerd" and "isCorrect" so I can easily track the statistics, I also implemented a "Save" button to save questions if you so choose.
I'm really having issues saving the information after the app closes, my questions are written like this:
data class Question(
val id : Int,
val question : String,
val correctAnswer: Int,
val hint : String,
val difficulty: String,
var isAnswered: Boolean,
var isCorrect : Boolean,
var isSaved : Boolean
)
and the questions are instantiated and saved like this:
object Constants{
const val DIFFICULTY: String = "difficutly"
var easyQuestionsAnswered = 0
var easyQuestionsAnsweredCorrectly = 0
var hardQuestionsAnswered = 0
var hardQuestionsAnsweredCorrectly = 0
var intermediateQuestionsAnswered = 0
var intermediateQuestionsAnsweredCorrectly = 0
val empty = ArrayList<Question>()
val easyQuestionsList = ArrayList<Question>()
val intermediateQuestionsList = ArrayList<Question>()
val hardQuestionsList = ArrayList<Question>()
val allQuestionsList = ArrayList<Question>()
val savedQuestions = ArrayList<Question>()
val que1 = Question(
1,
"בהינתן n/100 עצי AVL שבכל עץ יש 100 איברים, ניתן לבנות עץ AVL שמכיל את n האיברים ב (O(n",
0,
"study more",
"intermediate",
false,
false,
false
)
intermediateQuestionsList.add(que1)
when a user answers a question that questions fields are updated accordingly, however these changes are not saved when the app closes.
I've thought of creating a Boolean array for these fields and using the id's of the questions as the keys' and then updating the data when I load using sharedPrefrences, which im not even 100% sure on how to use but can probably manage. but this seems like a rather ugly way to do it and I was really hoping there is a simpler one.
I'm also having serious issues saving the ArrayList of questions called "savedQuestions", i tried using Gson but it just crashes when I open because of this line of code
val questions : ArrayList<Question> = Gson().fromJson<ArrayList<Question>>(json, type)
I tried following several guides but none of them seem to work for me and all lead to on boot crashes
Upvotes: 0
Views: 601
Reputation: 620
SharedPreference is a simpler way but with obvious disadvantages. Its only for smaller key/value pairs and also, if user uninstalls the app or delete the cached data, everything will be gone.
Use Firebase to store such key value/pairs on the server and then actually retrieve if the user answered it or not. Saving/retrieving is easier in such cases when the backend that does not involve any logic. If you just want to store the data locally, use room database which is much easier to write than the traditional way of creating MySqlite and SQLiteOpenHelper classes but at the same times provides support for offline storage quite easily.
If you are just want to save whether a question is answered, make sure that the question you get from server is always going to have the question identifier which you can use to check if which question was answered and yeah, this is not n ideal way because you can't be always sure what JSON response containing questions you will get from server and sometime server may be down as well.
val questions : ArrayList<Question> = Gson().fromJson<ArrayList<Question>>(json, type)
will only error if your question class does not contain any appropriate data types. So, without looking at the exact error stack trace and your json response, its difficult to predict what exactly is messy.
Upvotes: 1