pardom-zz / ActiveAndroid

Active record style SQLite persistence for Android
http://www.activeandroid.com
4.7k stars 1.03k forks source link

Load data from database and display on a ListView #210

Open pedrovinicius opened 10 years ago

pedrovinicius commented 10 years ago

How I can display the data from database query in a ListView?

joshuapinter commented 10 years ago

For a standard ListView or ListActivity, just use the toSql() method like so:

Client will be our model name.

String query = Client.all().orderBy("name").toSql();

Cursor cursor = ActiveAndroid.getDatabase().rawQuery(query, null);

ClientListCursorAdapter adapter = new ClientListCursorAdapter(this, R.layout.clients_listview_row, cursor, 0 );

this.setListAdapter(adapter);

Take a look at the following StackOverflow answer for a more in depth explanation: http://stackoverflow.com/a/20532937/293280