英文:
How to save and load keys of a custom object locally?
问题
我有一个自定义对象questionObject
,我从Firebase加载大约100个这样的对象并将它们存储在一个ArrayList<questionObject> result
中。然后,我按降序对列表result
进行排序,并将其传递给一个名为selectQuestionSet
的函数,在这个函数中,我必须提取前5个之前没有显示给用户的问题。我需要存储已经显示过的对象的键,并在下次选择另外5个问题时加载它们,以确保不重复。
我该如何实现这一点?我尝试使用SharedPreferences,但它只能存储基本数据类型,所以对我没有太大帮助。
以下是我的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;
}
// ... (其他方法和 compareTo 方法)
}
以下是我正在使用的函数:
public void selectQuestionSet(ArrayList<questionObject> result) {
int count = 0;
Collections.sort(result);
for (questionObject object : result) {
if (count < 4 && checkUsage(object.key)) {
// 显示问题
// 将键追加到已存储的列表
}
}
}
public boolean checkUsage(int key) {
// 在这里检查键是否已经使用过
return false;
}
在上述函数中,要获取result
对象中的5个问题,这些问题的key
不在存储的键列表中,您可以按如下方式进行操作:
public void selectQuestionSet(ArrayList<questionObject> result) {
int count = 0;
Collections.sort(result);
for (questionObject object : result) {
if (count < 5 && !checkUsage(object.key)) {
// 显示问题
// 将键追加到已存储的列表
count++;
}
}
}
这样,它将从result
中选择前5个key
不在已存储的键列表中的问题。请确保在checkUsage
函数中实现适当的逻辑来检查键是否已经使用过。
英文:
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?
答案1
得分: 1
一种方法可以是将键存储在SharedPreferences中。
由于键的类型是int
,您可以将它们存储在SharedPrefrences
中。
另一种方法是将对象序列化,然后将整个对象存储起来。有关详细信息,您可以参考https://stackoverflow.com/questions/28107647/how-to-save-listobject-to-sharedpreferences
英文:
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 https://stackoverflow.com/questions/28107647/how-to-save-listobject-to-sharedpreferences
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论