Android : How to add data to BaseAdapter from doInBackground() in onPostExecute()?

on Thursday, April 16, 2015


I have an AsyncTask where I'm getting a bunch of data through HTTP from an endpoint using the doInBackground method. I'm trying to pass that data into a CustomAdapter that extends BaseAdapter in the onPostExecute() method. But the data is not getting updated. Any idea what I might be doing wrong here?



public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {


View rootView = inflater.inflate(R.layout.fragment_main, container, false);

ListView listView = (ListView) rootView.findViewById(R.id.listview_activity);

EventsAdapter eventsAdapter = new EventsAdapter(getActivity(), objects);
objects.add(new EventsObject("M2arius", "Shaorma2"));

listView.setAdapter(eventsAdapter);

return rootView;
}

public class FetchEmailActivityTask extends AsyncTask<String, Void, String[]> {

private final String LOG_TAG = FetchEmailActivityTask.class.getSimpleName();
private final ProgressDialog dialog = new ProgressDialog(getActivity());

@Override
protected void onPreExecute() {
dialog.setMessage("Please Wait.");
dialog.setCancelable(true);
dialog.show();
}

@Override
protected String[] doInBackground(String... params) {

...
try {
return getActivityDataFromJson(activityJsonStr, limitParams);
} catch (JSONException e){
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
// This will only happen if there was an error getting or parsing the activity.
return null;
}

@Override
protected void onPostExecute(String[] result){
super.onPostExecute(result);
ArrayList<EventsObject> eventsList = new ArrayList<EventsObject>();
if (dialog.isShowing())
{
dialog.dismiss();
}

if (result != null){
objects.add(new EventsObject("Marius", "Shaorma"));
eventsAdapter = new EventsAdapter(getActivity(), objects);
if (objects != null) {
eventsAdapter.notifyDataSetChanged();
} else {
Log.v(LOG_TAG, "Objects: " + objects);
}
// New data is back from the server. Hooray!
}
}
}

0 comments:

Post a Comment