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();
}
}
}
}
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 classImageAdapter.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;
}
}
list.size()
value, where the list is not null and it is still not calledAny ideas how to resolve this?
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();
}
}
}
}