Passing Arraylist From Asynctask To Pageradapter
Solution 1:
what i want is to pass this list to the following class in order to use it in the imageView and textView in the viewPager
Then simply pass in the list as a parameter to the adapter and add a member variable for it. The usage of this adapter is at the bottom of this post, because I want to mention some other stuff.
classCustomAdapterextendsPagerAdapter{
private Context ctx;
private List<Student> data;
public CustomAdapter(Context ctx, List<Student> students) {
this.ctx = ctx;
this.data = students;
}
If you want to use that data
variable in the instantiateItem
method, then you can do Student s = this.data.get(position);
and use the various methods on the Student
object to load the TextView
or ImageView
.
Please note that you will need an image loading library (Picasso, Glide, Fresco, etc.) to load a URL into an ImageView
. While on the topic of libraries, though, you will save yourself much development time by looking into Gson for JSON parsing and Retrofit or Volley for HTTP network calls with JSON data.
As for your usage of the AsyncTask
, passing around the Activity
variable is bad practice. Try to use an asynchronous callback to the Activity instead.
publicinterfaceAsyncResponse<T> {
voidonResponse(T response);
}
publicclassConnectionAsyncTaskextendsAsyncTask<String, Void, List<Student>> {
privateAsyncResponse<List<Student>> callback;
publicConnectionAsyncTask(AsyncResponse<List<Student>> callback) {
this.callback = callback;
}
@OverrideprotectedList<User> doInBackground(String... params) {
String url = params[0];
final List<Student> students = newArrayList<Student>();
// TODO: JSON stuffreturn students;
}
@OverrideprotectedvoidonPostExecute(List<Student> result) {
if (this.callback != null) {
this.callback.onResponse(result);
} else {
Log.w("ConnectionAsyncTask", "Ignoring result");
}
}
}
publicclassSampleViewPagerActivityextendsActivity{
private ViewPager pager;
private PagerAdapter adapter;
private ArrayList<Student> students;
private ProgressDialog progress;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1. Inflate a layout
setContentView(R.layout.viewpager_activity);
// 2. Initialize the viewsthis.pager = (ViewPager) findViewById(R.id.pager);
this.progress = new ProgressDialog(this);
this.progress.setTitle("Loading");
this.progress.setMessage("Please wait");
// 3. Populate the views with datathis.students = new ArrayList<Student>();
this.adapter = new CustomAdapter(this, students);
this.pager.setAdapter(adapter);
// This code runs later, after 'execute' is called and the response is returned
ConnectionAsyncTask task = new ConnectionAsyncTask(new AsyncResponse<List<Student>>() {
@Overridepublic void onResponse(List<Student> response) {
students.clear();
students.addAll(response);
adapter.notifyDataSetChanged();
progress.hide();
}
});
// Optionally show some progress while waitingthis.progress.show();
// TODO: Use real URL
task.execute("http://www.somesite.com/data");
}
}
Post a Comment for "Passing Arraylist From Asynctask To Pageradapter"