1

I have a custom object questionObject and I load a list of about 100 such objects from firebase and store it in an ArrayList<questionObject> result. I then sort the list result in descending order and pass it to a function selectQuestionSet in which I have to extract the first 5 questions which haven't been previously shown to the user. I need to store the keys of the objects which I have already shown and load them the next time I have to select another 5 questions to ensure there is no repetition.

How do I achieve this? I tried using SharedPreferences but it stores only primitive datatypes so it wasn't of much use to me.

Here is my customObject:

public class questionObject implements Comparable<questionObject>{
    public String question;
    public String option1;
    public String option2;
    public String option3;
    public String option4;
    public String explanation;
    public String correct_attempts;
    public String total_attempts;
    public int key;


    public questionObject(String question, String option1, String option2, String option3, String option4, String explanation, String correct_attempts, String total_attempts, int key) {
        this.question = question;
        this.option1 = option1;
        this.option2 = option2;
        this.option3 = option3;
        this.option4 = option4;
        this.explanation = explanation;
        this.correct_attempts = correct_attempts;
        this.total_attempts = total_attempts;
        this.key = key ;

    }

    public String getQuestion() {
        return question;
    }

    public String getOption1() {
        return option1;
    }

    public String getOption2() {
        return option2;
    }

    public String getOption3() {
        return option3;
    }

    public String getOption4() {
        return option4;
    }

    public String getExplanation() {
        return explanation;
    }

    public String getCorrect_attempts() {
        return correct_attempts;
    }

    public String getTotal_attempts() {
        return total_attempts;
    }

    public int getKey() {
        return key;
    }

    @Override
    public int compareTo(questionObject comparestu) {
        float compareRatio= Integer.valueOf(((questionObject)comparestu).getCorrect_attempts ())/Integer.valueOf(((questionObject)comparestu).getTotal_attempts ());

        return Float.compare(compareRatio,  Integer.valueOf(this.total_attempts)/Integer.valueOf( this.total_attempts ) );

    }
}

And these are the functions I am using:

public void selectQuestionSet(ArrayList<questionObject> result){
        int count = 0;
        Collections.sort(result);
        for (questionObject object: result) {
            if(count < 4 && checkUsage ( object.key )){
                //display the question
                //append key to stored list
            }

        }
    }

    public boolean checkUsage(int key){
        //Check whether key already used or not here
        return false;
    }

What should I write in the above functions to get 5 questions from the result object whose key is not in the stored keys?

Ravish Jha
  • 481
  • 3
  • 25

1 Answers1

1

One approach could be to store the keys in SharedPrefernces

As the keys are of int type, you could store them in the SharedPrefrences

Or another approach would be serialize the object and then store the entire object. For that you can refer to How to save List<Object> to SharedPreferences?

DevHyperCoder
  • 895
  • 1
  • 9
  • 22