我有一个自定义对象mongodb-driver
,我从firebase加载了约100个此类对象的列表,并将其存储在questionObject
中。然后,我按降序对列表ArrayList<questionObject> result
进行排序,并将其传递给函数result
,在该函数中,我必须提取之前未向用户显示的前5个问题。我需要存储已经显示的对象的键,并在下次必须选择另外5个问题以确保不重复时加载它们。
我该如何实现?我尝试使用SharedPreferences,但它仅存储原始数据类型,因此对我来说用处不大。
这是我的customObject:
selectQuestionSet
这些是我正在使用的功能:
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;
}
不在存储的键中的result
对象中获得5个问题?
答案 0 :(得分:1)
一种方法可能是将密钥存储在SharedPrefernces中
由于密钥属于SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(STAFFID_, ',', 3),',',-1)
类型,因此可以将其存储在int
或者另一种方法是序列化对象,然后存储整个对象。为此,您可以参考How to save List<Object> to SharedPreferences?