Reputation: 193
I've SQLite database table like this :
_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 ||
How do I select one random of Column2 from this table?
I want the result like this :
_id || Column1 || Column2 ||
1 || test1 || d ||
2 || test2 || k ||
3 || test3 || r ||
assumed that d, k, r is random value of Column2 for each records.
Thanks
UPDATES : I've create CustomAdapter and add some View like this :
@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);
}
}
Now, just use that customAdapter on your activity, so that column2 can be show as 1 random value.
Upvotes: 0
Views: 564
Reputation: 6409
This would be much easier to do in Java once you have the data from the DB.
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)];
Upvotes: 2