0

Similar question to Custom adapter getview is not called

I have a custom adapter to put images on my app screen but it is not being called at all. Doing some research I found that I needed to override a getCount() method. I have done this returning the size of the ArrayList, but still no luck. I thought the list could have been empty, but through debugging, I can confirm it is not when it is passed through to the custom ArrayAdapter class. I even tried returning a value of 20 in getCount() to see if that would work, but still no luck.

Here is the code I have done so far;

MainActivityFragment.class

public class MainActivityFragment extends Fragment {
    private ImageAdapter imageAdapter;

    public class FetchMovieTask extends AsyncTask<String, Void, JSONObject> {
        @Override
        protected void onPostExecute(JSONObject strings) {


            Log.i(LOG_TAG, strings.toString());

            try {
                jsonArray = strings.getJSONArray("results");


            if (null != jsonArray) {

                ArrayList<JSONObject> list = new ArrayList<JSONObject>();

                if (jsonArray != null) {
                    int len = jsonArray.length();
                    for (int i=0;i<len;i++){
                        list.add((JSONObject) jsonArray.get(i));
                    }
                }

                imageAdapter = new ImageAdapter(getActivity(), list);
            }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
  • There is a lot more to this fragment class, but I just included where I return a JSONObject from doInBackground() and in onPostExecute() I extract a JSONArray from the JSONObject and put each element from the JSONArray into an ArrayList<JSONObject> and send that to my custom ArrayAdapter class

ImageAdapter.class

public class ImageAdapter extends ArrayAdapter<ArrayList> {

ArrayList list;
Context context;

public ImageAdapter(Context context, ArrayList list) {
    super(context, android.R.id.content);
    this.list = list;
    this.context = context;
}


@Override
public int getCount() {
    //return list.size();
    return 20;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    String IMAGE_BASE_URL = "http://image.tmdb.org/t/p/w185/";

    try {
        JSONObject jsonObject = (JSONObject) list.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_image_movie, parent, false);
        }

        ImageView iv = (ImageView) convertView.findViewById(R.id.list_movie_pics_imageview);

        Picasso.with(context)
                .load(IMAGE_BASE_URL + jsonObject.get("poster_path"))
                .into(iv);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return convertView;
}

}
  • getCount has been set to the list.size() value, where the list is not null and it is still not called
  • It has been hardcoded to 20 (the size of the list) and it is still not called

Any ideas how to resolve this?

Community
  • 1
  • 1
Dan
  • 1,736
  • 3
  • 21
  • 45
  • 1
    where are you calling setAdapter ? Check if [this](http://stackoverflow.com/questions/16338281/custom-adapter-getview-method-is-not-called/16338380#16338380) helps – Blackbelt Jan 21 '16 at 12:14
  • I am calling it in the `onCreateView` method – Dan Jan 21 '16 at 12:22

3 Answers3

1

After intializing your Adapter set it to your ListView

public class MainActivityFragment extends Fragment {

private ImageAdapter imageAdapter;

public class FetchMovieTask extends AsyncTask<String, Void, JSONObject> {
    @Override
    protected void onPostExecute(JSONObject strings) {


        Log.i(LOG_TAG, strings.toString());

        try {
            jsonArray = strings.getJSONArray("results");


        if (null != jsonArray) {

            ArrayList<JSONObject> list = new ArrayList<JSONObject>();

            if (jsonArray != null) {
                int len = jsonArray.length();
                for (int i=0;i<len;i++){
                    list.add((JSONObject) jsonArray.get(i));
                }
            }

            imageAdapter = new ImageAdapter(getActivity(), list);
            yourListView.setAdapter(imageAdapter);
        }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
  }
}
Chintan Bawa
  • 1,266
  • 8
  • 15
  • Perfect :) I had set the adapter in onCreateView but declared the imageAdapter in onPostExecute. When I moved it down to onPostExecute it worked. Thanks – Dan Jan 21 '16 at 12:30
0

You have to call imageAdapter.notifyDataSetChanged() after creating this. This will notify the adapter to call getView()

UPDATE Set adapter on listView and then notifyDataSetChanged

yourListView.setAdapter(imageAdapter); imageAdapter.notifyDataSetChanged()

Mustansar Saeed
  • 2,458
  • 2
  • 17
  • 39
0

As per your comment I can see you are assigning your adapter to the listview in onCreateView method. Your adapter is not ready by then. So you need to refresh your adapter after adding data to it.

Add

imageAdapter.notifyDataSetChanged();

After

imageAdapter = new ImageAdapter(getActivity(), list);

in onPostExecute method.

Rohit5k2
  • 16,859
  • 8
  • 38
  • 56