我正在 Android 中创建一个简单的多项选择琐事游戏,其中用户有三个选择。正确答案选项直接从数据库中提取,另外两个错误选项使用方法随机选择。我将正确和 2 个错误的答案放在一个数组中,然后对数组进行洗牌。当在随机选择方法中我只从行中抓取一列时,游戏运行良好。现在我想从每个随机行中抓取 3 列。那么我的问题是如何编写“获取两个随机三列的行并将生成的数组放入一个可以洗牌的数组”的代码。这是我的工作代码:
DBAdapter:
// ---Grabs 2 RANDOM ---
public String[] getRandomHA() {
Cursor cursor = this.db.query(
"hdtable Order BY RANDOM() LIMIT 2",
new String[] { KEY_HA }, null, null, null, null, null);
if (cursor != null) {
int i = 0;
String colStrings[] = new String[cursor.getCount()];
while (cursor.moveToNext())
{
colStrings[i] = cursor.getString(0);
i++;
//cursor.close();
}
return colStrings;
}
return null;
}
// --- END Grabs 2 RANDOM ---
Main Activity:
// --- Random 2 answers ----
public void getRandom() {
dba.open();
String[] wrong2 = dba.getRandomHA();
dba.close();
}
shuffle an Array:
// --- shuffle array ----
String[] numbArray = { rightAnswer[2], wrong2[0], wrong2[1] };
List<String> aList = new ArrayList<String>();
for (String s : numbArray)
aList.add(s);
Collections.shuffle(aList);
TextView[] tVs = { optionA_TV, optionB_TV, optionC_TV };
for (int i = 0; i < tVs.length; i++) {
tVs[i].setText(aList.get(i));
}
// --- END shuffle array ----
那么这就是我编写数据库适配器方法的方式吗?
// ---Grabs 2 RANDOM ---
public String[] getRandomHA() {
Cursor cursor = this.db.query(
"hdtable Order BY RANDOM() LIMIT 2",
new String[] { KEY_Y, KEY_MD, KEY_HA }, null, null, null, null, null);
if (cursor != null) {
int i = 0;
String colStrings[] = new String[cursor.getCount()];
while (cursor.moveToNext())
{
colStrings[i] = cursor.getString(0);
colStrings[i] = cursor.getString(1);
colStrings[i] = cursor.getString(2);
i++;
//cursor.close();
}
return colStrings;
}
return null;
}
// --- END Grabs 2 RANDOM ---
如果上述方法是正确的,那么我将如何编写主要活动方法来反映嵌套数组?