私はこのようなSQLiteデータベーステーブルを持っています:
_id || Column1 || Column2 ||
1 || test1 || a,b,c,d,e,f ||
2 || test2 || g,h,i,j,k,l ||
3 || test3 || m,n,o,p,q,r ||
このテーブルからランダムに Column2 を 1 つ選択するにはどうすればよいですか?
私はこのような結果が欲しい:
_id || Column1 || Column2 ||
1 || test1 || d ||
2 || test2 || k ||
3 || test3 || r ||
d、k、r は各レコードの Column2 のランダムな値であると仮定します。
ありがとう
更新: CustomAdapter を作成し、次のようなビューを追加しました:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int column2Name= c.getColumnIndex(DBAdapter.COLUMN2);
String col2Name= c.getString(column2Name);
String[] temp;
Random random = new Random();
temp = col2Name.split(",");
String col2 = temp[random.nextInt(temp.length)];
TextView col2_name = (TextView) v.findViewById(R.id.column2_label);
if (col2_name != null) {
col2_name .setText(col2);
}
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
int column2Name= c.getColumnIndex(DBAdapter.COLUMN2);
String col2Name= c.getString(column2Name);
String[] temp;
Random random = new Random();
temp = col2Name.split(",");
String col2 = temp[random.nextInt(temp.length)];
TextView col2_name = (TextView) v.findViewById(R.id.column2_label);
if (col2_name != null) {
col2_name .setText(col2);
}
}
次に、その customAdapter をアクティビティで使用して、column2 を 1 つのランダムな値として表示できるようにします。