F_X F_X - 6 months ago 11
Android Question

How to display database RANDOM in android listview

anyone know hot to display random data from database in mysql and display in listview?

i can display all data without random, but i want to displayed it random, anyone can help?

my code :

for (int i = 0; i < response.length() ; i++) {

try {
JSONObject obj = response.getJSONObject(i);
Exercise exercise = new Exercise();
if (obj.getString("KindOf").equals(textKind.getText().toString()) && obj.getString("Type").equals("Strength")) {
exercise.setTipe(obj.getString("Type"));
exercise.setJenis(obj.getString("KindOf"));
exercise.setNama(obj.getString("Name"));


exerciseList.add(exercise);

} catch (JSONException e) {
e.printStackTrace();
}

}

Answer

If you want to shuffle an ArrayList, you can just use the Collections shuffle method.

Collections.shuffle(exerciseList);

Or

SELECT *
FROM excercises
ORDER BY RAND();

If you want it at DB level.