Dan
Dan

Reputation: 2100

getView of custom ArrayAdapter not being called

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();
            }
        }
    }
}

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;
}

}

Any ideas how to resolve this?

Upvotes: 0

Views: 817

Answers (3)

Rohit5k2
Rohit5k2

Reputation: 18112

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.

Upvotes: 0

Chintan Bawa
Chintan Bawa

Reputation: 1386

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();
        }
    }
  }
}

Upvotes: 1

Mustansar Saeed
Mustansar Saeed

Reputation: 2790

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()

Upvotes: 0

Related Questions