I would like to know how to load the data from a sql query in a list view. No matter how hard I try I can't do it :S
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table usuarios(nombre text primary key, edad int, puntuacion int)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table usuarios");
db.execSQL("create table usuarios(nombre text primary key, edad int, puntuacion int)");
}
public class Ranking extends AppCompatActivity{
private ListView lv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ranking);
lv1 = (ListView)findViewById(R.id.lv1);
ArrayList <String> ranking = new ArrayList<>();
AdminSQLiteOpenHelper juego = new AdminSQLiteOpenHelper(this, "juego", null, 5);
SQLiteDatabase bd = juego.getWritableDatabase();
Cursor fila = bd.rawQuery("select nombre, puntuacion from usuarios", null);
if(fila.moveToFirst()){
fila = bd.rawQuery("select nombre, puntuacion from usuarios", null);
ranking.add(fila.getString(0) + " - " + fila.getString(1));
while(fila.moveToNext()){
ranking.add(fila.getString(0) + " - " + fila.getString(1));
}
}
bd.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ranking);
lv1.setAdapter(adapter);
}
}
Try this: