Android 如何在一个字段中随机选择一条记录
我创建了如下SQLite数据库表:Android 如何在一个字段中随机选择一条记录,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我创建了如下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 || _id || Column1 || Column2 || 1 || test1 || d
_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 ||
_id || Column1 || Column2 ||
1 || test1 || d ||
2 || test2 || k ||
3 || test3 || r ||
@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);
}
}
如何从该表中随机选择第2列中的一个
我想要这样的结果:
_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 ||
_id || Column1 || Column2 ||
1 || test1 || d ||
2 || test2 || k ||
3 || test3 || r ||
@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);
}
}
假设d,k,r是每个记录第2列的随机值
谢谢
更新:
我创建了CustomAdapter并添加了一些视图,如下所示:
_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 ||
_id || Column1 || Column2 ||
1 || test1 || d ||
2 || test2 || k ||
3 || test3 || r ||
@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个随机值。一旦从数据库中获得了数据,在Java中就更容易做到这一点
Random RANDOM = new Random(System.currentTimeMillis());
int index = cursor.getColumnIndex("Column2");
String value = cursor.getString(index);
String[] values = value.split(",");
String randomValue = values[RANDOM.nextInt(values.length)];
尝试
按random()限制1从表顺序中选择列代码>不,我的意思是,不是所有的列都随机显示,只是列2YUP yup上的记录,或者我不想所有的行都随机选择,只是列2的值,随机选择1,就像我说的按随机顺序从表中选择列2()限制1代码>看第一个示例:))