使用
List<String> notizen = new ArrayList<String>();
SQLiteDatabase connection;
connection = openOrCreateDatabase(
dbPath
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
Cursor cursor = connection.rawQuery("SELECT [subject],[note],[priority] FROM tasks WHERE [c] = 'test'",null);
try {
if (cursor.moveToFirst()) {
do {
if (cursor.getString(1).toString().length() > 10)
notizen.add(cursor.getString(0).toString() + "#" + cursor.getString(1).toString() + "#" + cursor.getString(2).toString());
else
notizen.add(cursor.getString(0).toString() + "#" + cursor.getString(2).toString());
} while (cursor.moveToNext());
}
}
catch(Exception dd)
{
int duration = Toast.LENGTH_LONG;
Context context = getApplicationContext();
CharSequence text = "Erra: "+dd.getMessage();
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
connection.close();
我将subject and note and priority
或subject and priority
插入到字符串列表中。
我想从此列表中选择一个随机条目,但应该有一个加权函数。根据优先级,元素应更频繁地出现。 1等于最频繁,5等于最不频繁(通常是一次)。
应该有一个临时列表,例如:
subject1#note1#priority1
subject2#note2#priority2
subject5#note5#priority5
subject1#note1#priority1
subject2#note2#priority2
subject2#note2#priority2
subject1#note1#priority1
subject1#note1#priority1
subject1#note1#priority1
subject2#note2#priority2
请注意,条目是随机混合的,subject1出现5次,而subject5出现一次。
我该怎么做?
答案 0 :(得分:0)
试试这个:
class Element{
int priority;
String data;
}
ArrayList<Element> elements = createCoreElements();
ArrayList<Element> randomItems = new ArrayList<Element>();
for(Element element : elements){
randomItems.add(element); // in case you want to add minimum amount of elements from each type
for(int i = 0; i < element.priority; i++){
if(Math.random() < 0.5){// random boolean
randomItems.add(element);
}
}
}
Collections.shuffle(randomItems);
print(randomItems);
答案 1 :(得分:-1)
我自己解决了这个问题:
try {
if (cursor.moveToFirst()) {
do {
if (cursor.getString(1).length() > 10) {
if (cursor.getString(2).equals("5")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
}
if (cursor.getString(2).equals("4")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
}
if (cursor.getString(2).equals("3")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
}
if (cursor.getString(2).equals("2")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
}
if (cursor.getString(2).equals("1")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(1) + "#" + cursor.getString(2));
}
}
else {
if (cursor.getString(2).equals("1")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
}
if (cursor.getString(2).equals("2")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
}
if (cursor.getString(2).equals("3")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
}
if (cursor.getString(2).equals("4")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
}
if (cursor.getString(2).equals("5")) {
notizen.add(cursor.getString(0) + "#" + cursor.getString(2));
}
}
} while (cursor.moveToNext());
}
}
catch(Exception dd)
{
//
}
connection.close();