Skip to content

Instantly share code, notes, and snippets.

@methodin
Created May 30, 2013 15:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methodin/5678832 to your computer and use it in GitHub Desktop.
Save methodin/5678832 to your computer and use it in GitHub Desktop.
Using an AsyncLoader with ActiveAndroid
/*
* Example:
* new ListLoader(listView).execute(new Select().from(TestEntity.class));
*/
import java.lang.ref.WeakReference;
import java.util.List;
import android.os.AsyncTask;
import android.widget.ListView;
import com.activeandroid.query.From;
public class ListLoader extends AsyncTask<From, Void, List<TestEntity>> {
private final WeakReference<ListView> referenceView;
public ListLoader(ListView listView) {
referenceView = new WeakReference<ListView>(listView);
}
@Override
protected List<TestEntity> doInBackground(From... params) {
return params[0].execute();
}
/**
* The system calls this to perform work in the UI thread and delivers the
* result from doInBackground()
*/
@Override
protected void onPostExecute(List<TestEntity> items) {
final ListView listView = referenceView.get();
if(listView != null) {
TestListAdapter adapter = (TestListAdapter) listView.getAdapter();
adapter.setItems(items);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment