我正在制作一个测验应用程序,其中的问题和答案取自firebase。我已经在firebase中上传了其选项的问题,现在想要随机检索它们但不知道怎么做,因为我是编程的初学者。 这是我的firebase数据的屏幕截图
https://drive.google.com/file/d/1T7-x3TP1TaA8_ntwfoRNdb2oMGV_swl6/view?usp=sharing
CMakeLists.txt
这是我从firebase按顺序加载问题的代码!我google了很多但没有得到如何生成一个随机节点并获得该随机节点的子值!
答案 0 :(得分:0)
作为初学者,产生随机数可能是一个小混乱。因此,一种简单的方法是将当前时间置于毫秒中,使其看起来像11023049
(小时 - 分 - 秒 - ms)
由于每次生成时它都是唯一的,因此您需要一个函数来确定基于此唯一值检索的问题顺序。
要特定于您的问题,您可以在毫秒中获得时间,因为它是变化最快的变量并检索其数量等于毫秒的问题。或者要有一些变化,奇数毫秒的值会以相反的顺序提取问题,甚至值也会按正常顺序获取。
更新1 : 创建订单的一个简单方法是(这只会使起始问题随机化,下一个问题将按顺序排列,因此强烈建议使用其他方法):
public void ArrayList<Integer> getOrderTrivial(int lastQsNum)
{
System.out.println("Started");
int currentSec = Integer.parseInt(new SimpleDateFormat("ss").format(new Date()));
int count = 0;
ArrayList<Integer> order = new ArrayList<Integer>();
if (currentSec%2 == 0)
{
/*If the second value is even, the order of questions will be straight*/
for (int i=currentSec; i<=lastQsNum; i++)
{
order.add(i);
}
for (int i=0; i<=currentSec; i++)
{
order.add(i);
}
}
else
{
/*If the second value is even, the order of questions will be reverse*/
for (int i=currentSec; i>=0; i--)
{
order.add(i);
}
for (int i=lastQsNum; i>=currentSec; i--)
{
order.add(i);
}
}
System.out.println("Complete");
return order;
}
更新2:更好的方法(推荐)
public ArrayList<Integer> getOrder(int lastQsNum)
{
ArrayList<Integer> order = new ArrayList<>();
int sec = Integer.parseInt(new SimpleDateFormat("ss").format(new Date()));
int start = sec, down = sec - 1, up = sec + 1;
order.add(start);
while (up<=lastQsNum && down>=0)
{
if (!order.contains(up)) order.add(up);
if (!order.contains(down)) order.add(down);
up = up+1;
down = down-1;
}
if (up>lastQsNum)
{
System.out.println("Upper Limit Reaached: "+up+"-"+down);
int mid = down/2;
if (!order.contains(mid)) order.add(mid);
up = mid+1;
down = mid -1;
}
else if (down<0)
{
System.out.println("Lower Limit Reaached: "+up+"-"+down);
int mid = up + ((lastQsNum - up)/2);
if (!order.contains(mid)) order.add(mid);
up = mid+1;
down = mid -1;
}
while (up<=lastQsNum && down>=0)
{
if (sec%3 == 0)
{
if (!order.contains(up)) order.add(3, up);
if (!order.contains(down)) order.add(6, down);
}
else if (sec%5 == 0)
{
if (!order.contains(up)) order.add(5, up);
if (!order.contains(down)) order.add(10, down);
}
else if (sec%7 == 0)
{
if (!order.contains(up)) order.add(7, up);
if (!order.contains(down)) order.add(14, down);
}
else if (sec%11 == 0)
{
if (!order.contains(up)) order.add(11, up);
if (!order.contains(down)) order.add(22, down);
}
else if (sec%2 == 0)
{
if (!order.contains(up)) order.add(0, up);
if (!order.contains(down)) order.add(0, down);
}
else
{
if (!order.contains(up)) order.add(up);
if (!order.contains(down)) order.add(down);
}
up = up+1;
down = down-1;
}
return order;
}
两种方法的返回对象都是ArrayList。
在Activity的onCreate()
存根中,您应该运行此函数并创建一个ArrayList。类似的东西:
//Global Variables
ArrayList<Integer> order;
int maxQuestions = 200;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
order = getOrderTrivial(maxQuestions);
}
创建名为ArrayList
的{{1}}后,您可以串行遍历order
并获取问题编号并使用它从ArrayList
(来自ValueEventListener)获取问题)。
答案 1 :(得分:0)
我一直在考虑如何做到这一点,我来到了这个
首先,您需要从Database
获取所有数据,因为这样做如下所示
DatabaseReference ref= FirebaseDatabase.getInstance().getReference();
//Since you dont have a parent for all the questions, you should reference to your url to get all the items
ref.child("https://testapp-465fe.firebaseio.com/").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
questionList = new ArrayList<String>();
// Result will be holded Here
for (DataSnapshot ql : dataSnapshot.getChildren()) {
questionList.add(String.valueOf(ql.geValue())); //We add all the results to an arraylist
}
/* Now all the data from your database will be stored in the ArrayList, just go and get from there what you need, the answers, the questions, etc */
我将为您提供有关如何从ArrayList
获取内容的摘要 /*This method retrieves values from ArrayList using Iterator
*/
public static void retrieveValuesFromList(List list)
{
Iterator itr = list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next()); //you can use a Log.e("Data: ",""+itr.next()); here
}
}
其中list
将是您的ArrayList
questionList
retrieveValuesFromList(yourlist);
希望这有帮助
快乐的编码!