Seef Seef - 11 months ago 43
Android Question

Can't get custom list adapter to populate listview using asynctask

I'm trying to populate an ArrayList of objects and use those objects to populate a ListView. My Asynctask can get the json data and I can parse it and make the objects I need but my ListView doesn't populate. When I check to see if my ArrayList has any object in it before the adapter runs I can see that it doesn't. I want to know why my ListView isn't populating.

Here's my code: (Sorry if it's messy, some spots I haven't gotten to updating yet)

public class MovieDisplayFragment extends Fragment{

private ArrayList<Movie> movieList = new ArrayList<Movie>();
private MovieAdapter movieAdapter;
ListView listView;

public MovieDisplayFragment(){

public void onCreate(Bundle savedInstanceState){

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

movieAdapter = new MovieAdapter(getActivity(), movieList);
listView = (ListView) rootView.findViewById(;

if(movieList.size() > 0) {
Log.e("Hello", "1");

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
Movie movie = movieAdapter.getItem(position);
Intent i = new Intent(getActivity(), DetailActivity.class)
.putExtra(Intent.EXTRA_TEXT, "Hello");

return rootView;

private void updateMovieData(){
getMovieData movieData = new getMovieData();

public void onStart(){

public class getMovieData extends AsyncTask<Void, Void, List<Movie>> {

private final String LOG_CAT = getMovieData.class.getSimpleName();

private List<Movie> getMovieData(String movieJsonStr) throws JSONException {

final String MOV_ITEMS = "results";
final String MOV_TITLE = "original_title";
final String MOV_DATE = "release_date";
final String MOV_SYNOPSIS = "overview";
final String MOV_VOTE = "vote_average";
final String MOV_POSTER_URL = "poster_path";

JSONObject movieJson = new JSONObject(movieJsonStr);
JSONArray movieArray = movieJson.getJSONArray(MOV_ITEMS);

Log.e("Hello", "2");

for (int i = 0; i < movieArray.length(); i++) {

JSONObject movie = movieArray.getJSONObject(i);

movieList.add(new Movie(movie.getString(MOV_TITLE), movie.getString(MOV_DATE),
movie.getString(MOV_SYNOPSIS), movie.getString(MOV_VOTE), movie.getString(MOV_POSTER_URL)));


return movieList;

protected List<Movie> doInBackground(Void... params) {

HttpURLConnection urlConnection = null;
BufferedReader reader = null;

String movieJsonStr = null;

try {

final String BASE_URL = "";
Uri builtUri = Uri.parse(BASE_URL).buildUpon().build();
URL url = new URL(builtUri.toString());

Log.v(LOG_CAT, "Built URI " + builtUri.toString());

urlConnection = (HttpURLConnection) url.openConnection();

InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null){
movieJsonStr = null;
reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while((line = reader.readLine()) != null){
buffer.append(line + "\n");

if (buffer.length() == 0){
movieJsonStr = buffer.toString();
movieJsonStr = buffer.toString();

Log.v(LOG_CAT, "Movie String: " + movieJsonStr);
} catch (IOException e) {
Log.e("Fragment", "Error", e);
movieJsonStr = null;

} finally {
if (urlConnection != null) {
if (reader != null) {
try {
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);

try {
return getMovieData(movieJsonStr);
} catch (JSONException e) {
Log.e(LOG_CAT, e.getMessage(), e);
return null;

protected void onPostExecute(List<Movie> movies){
if(movies != null){
for(Movie movieData : movies){


Answer Source

Put these 2 lines inside your onPostExecute() in Async.

movieAdapter = new MovieAdapter(getActivity(), movieList);

AsyncTask runs in Background Thread. It gets the data from json after few seconds. But your adapter is called few milli seconds after your fragment is created.

So the data from the Json will not be there when you are setting the adapter.

Calling it in onPostExecute solves this problem as the adatpter is set after Json data is retrieved from the server!

Hope it helps a bit.