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