Created
May 30, 2013 15:36
-
-
Save methodin/5678832 to your computer and use it in GitHub Desktop.
Using an AsyncLoader with ActiveAndroid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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