我只想要合乎逻辑的帮助。我在数据库中有 100 行,并随机获取所有行,而不会重复我在下面的代码中使用过。
public ArrayList<Rows> getRows(){
ArrayList<Rows> myRaw=new ArrayList<Rows>();
Cursor rawCursor=this.db.query(DB_TABLE, null, null, null, null, null, null);
if(rawCursor.getCount()>0){
Random rng = new Random();
rawCursor.moveToFirst();
do {
// Ideally just create one instance globally
ArrayList<Rows> generated = new ArrayList<Raws>();
for (int i = 0; i < 371; i++)
{
while(true)
{
Integer next = rng.nextInt(371) + 1;
rawCursor.moveToPosition(next);
Rows raw=new Raws(rawCursor.getInt(rawCursor.getColumnIndex("_id")),rawCursor.getString(rawCursor.getColumnIndex("raw")),rawCursor.getInt(rawCursor.getColumnIndex("fav")));
if (!generated.contains(raw))
{
// Done for this iteration
generated.add(raw);
break;
}
}
}
myRaw=generated;
} while (rawCursor.moveToNext());
rawCursor.close();
}
return myRaw;
}
谢谢大家