package com.example.ormlite.cursor; import android.app.Activity; import android.app.LoaderManager; import android.content.Loader; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import com.example.ormlite.cursor.R; import com.example.ormlite.cursor.OrmLiteCursorLoader; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; import java.sql.SQLException; /** * Created by andrei on 30/01/15. */ public class UsageActivity extends Activity implements LoaderManager.LoaderCallbacks { public static final int CURSOR_LOADER_ID = "CURSOR_LOADER_ID".hashCode(); private Dao mDeviceModelDao; private PreparedQuery mYourAwesomeQuery; private DeviceCursorAdapter mDeviceCursorAdapter; private ListView mDevicesListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.your_awesome_activiy_layout); this.mDevicesListView = (ListView) this.findViewById(R.id.your_awesome_devices_list_view); this.mDeviceModelDao = ((AwesomeApplication)getApplicationContext()).getDatabaseHelper().getDeviceModelDao(); try { this.mYourAwesomeQuery = mDeviceModelDao.queryBuilder().selectColumns("name","_id").prepare(); } catch (SQLException e) { Log.e(this.getClass().getSimpleName(), e.getMessage()); } this.mDeviceCursorAdapter = new DeviceCursorAdapter(this); this.mDevicesListView.setAdapter(mDeviceCursorAdapter); this.getLoaderManager().initLoader(CURSOR_LOADER_ID, null, this); } @Override public Loader onCreateLoader(int id, Bundle args) { return new OrmLiteCursorLoader<>(this, mDeviceModelDao, mYourAwesomeQuery); } @Override public void onLoadFinished(Loader loader, Cursor data) { this.mDeviceCursorAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader loader) { this.mDeviceCursorAdapter.swapCursor(null); } }