You are calling photoUrlList.add()
in your onSuccess()
method. That, plus your symptoms, suggests that you are not starting with a fresh list on every refresh operation, so you keep appending the same URL to the ArrayList
on each refresh.
GridView adapter populating incorrectly onRefesh of SwipeRefreshLayout
-
17-07-2023 - |
Question
I am having an issue with my GridView adapter - the first load works properly but every swipe to reload adds the first image into the empty grid view spots even know there is only one photo in the result.
Pictures will help illustrate.
So I suspect my GridAdapter isn't handling the data correctly, but not sure what I'm missing.
This is my onSuccess which calls the adapter after AsyncHttpClient grabs data from the API:
@Override
public void onSuccess(JSONObject jsonObject) {
swipeLayout.setRefreshing(false);
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
JSONArray list = jsonObject.optJSONArray("photos");
for (int i = 0; i < list.length(); i++){
try {
photoUrlList.add(list.getJSONObject(i).getString("thumb_url"));
} catch (JSONException e) {
e.printStackTrace();
}
}
mFourGridAdapter.updateData(photoUrlList);
}
My MyFourGridAdapter:
private class MyFourGridAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private List<String> photoUrlList;
public MyFourGridAdapter(Context context, List<String> photoUrlList ) {
inflater = LayoutInflater.from(context);
this.photoUrlList = photoUrlList;
}
public void updateData(List<String> values) {
// update the adapter's dataset
photoUrlList = values;
notifyDataSetChanged();
}
@Override
public int getCount() {
return photoUrlList.size();
}
@Override
public Object getItem(int i)
{
return photoUrlList.get(i);
}
@Override
public long getItemId(int i)
{
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
if(v == null) {
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.frame_square_image_view, v.findViewById(R.id.frame_square_image_view));
}
picture = (ImageView)v.getTag(R.id.frame_square_image_view);
String photoUrl = photoUrlList.get(i);
Picasso.with(picture.getContext()).load(photoUrl).into(picture);
return v;
}
}
Please comment if anything is unclear - its a bit hard to explain. Any help is appreciated, thanks.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow