我有一个自定义对象questionObject
,我从firebase加载大约100个这样的对象的列表,并将其存储在ArrayList<questionObject> result
中。然后,我按降序对列表result
进行排序,并将其传递给函数selectQuestionSet
,在该函数中,我必须提取之前没有向用户显示的前5个问题。我需要存储我已经显示的对象的密钥,并在下次我必须选择另外5个问题时加载它们,以确保不会重复。
我该如何做到这一点?我试着使用SharedPreferences,但它只存储原始数据类型,所以对我没有多大用处
这是我的客户对象:
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 ) );
}
}
这些是我正在使用的功能:
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;
}
如果key
不在存储的密钥中,那么我应该在上面的函数中写些什么来从result
对象中获得5个问题?
一种方法可以是将密钥存储在SharedPreferences 中
由于密钥是int
类型的,您可以将它们存储在SharedPrefrences
中
或者另一种方法是序列化对象,然后存储整个对象。您可以参考如何保存列表<对象>到SharedPreferences?