moayed ayasaa moayed ayasaa - 2 months ago 19
JSON Question

Parsing data from Json as a collection of image to Picasso

I tried to parse a collection of image to

recyclerview
as
gridview
using
Picasso
library but i cannot make it work

Note : i dont have any error but not working
Here's Json data a link

My module

public class ImagesModule

{
public ArrayList getListTest() {
return listTest;
}

public void setListTest(ArrayList listTest) {
this.listTest = listTest;
}

ArrayList listTest = new ArrayList( );

}


this is the Adapter`

public class ImageViewAdapter extends RecyclerView.Adapter<ImageViewAdapter.ViewHolder> {
List<ImagesModule> imagesModules;
Context context;

public ImageViewAdapter(List<ImagesModule> imagesModules, Context context){
super();
this.imagesModules = imagesModules;
this.context = context;}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.imageitem, parent,false );
ViewHolder viewHolder = new ViewHolder( v );

return viewHolder;}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ImagesModule imagesModule = imagesModules.get( position );
Picasso.with(context).load(String.valueOf(imagesModule.getListTest())).into(holder.appImage);}
@Override
public int getItemCount() {
return imagesModules.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public android.widget.ImageView appImage;
public ViewHolder(View itemView) {
super( itemView );
appImage = (android.widget.ImageView) itemView.findViewById( R.id.imagegallary);
}}}


and this is the fragment

public class ImageViewFragment extends Fragment {
List<ImagesModule> imagesModules;


List<ImagesModule> imagesModule;
RecyclerView AppRecyclerView;
RecyclerView.Adapter imageRecyclerViewadapter;
String jsonUrl = "https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json";
RequestQueue requestQueue;
private RecyclerView.LayoutManager mLayoutManager;
public ImageViewFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_image_view, container, false);
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppRecyclerView = (RecyclerView) getView().findViewById(R.id.imageRecyclerView);
imagesModule = new ArrayList<>();
imagesModules = new ArrayList<>();
JsonAppShowData();
}
public void JsonAppShowData() {
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( jsonUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
ImagesModule imagesModule = new ImagesModule();

JSONArray jsonArray = response.getJSONObject("feed").getJSONArray("entry");
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray imageArray = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONArray("im:image");
for (int j =0; j < imageArray.length(); j++) {
ArrayList listTest = new ArrayList( );
String image = imageArray.getJSONObject(j).getString("label");
listTest.add(image);
imagesModule.setListTest(listTest);
imagesModules.add(imagesModule);}}

imageRecyclerViewadapter = new ImageViewAdapter(imagesModules,getContext());
AppRecyclerView.setAdapter(imageRecyclerViewadapter);
} catch (JSONException e) {e.printStackTrace();
}}}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e( "LOG", error.toString() );}});
requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonObjectRequest);
mLayoutManager = new GridLayoutManager(getContext().getApplicationContext(),3);
AppRecyclerView.setLayoutManager(mLayoutManager);}}

Answer

You would need a List<String> imageUrls to save urls to be viewed by picasso, no need for special object for that. To solve it, change your json parsing code to

try {
    JSONArray entries = response.getJSONObject("feed").getJSONArray("entry");
    int count = entries.length();
    for (int i = 0; i < count; i++) {
        JSONObject imageJson = entries.getJSONObject(i).getJSONObject("im:image");
        // in case you want to get image with height 53
        String imageUrl = imageJson.getJSONObject("0").getString("label");

        // String imageUrl = imageJson.getJSONObject("1").getString("label"); height 75
        // String imageUrl = imageJson.getJSONObject("2").getString("label"); height 100

        // add urls to the list
        imageUrls.add(imageUrl);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

then change your adapter to have a List<String> imageUrls instead of List<ImagesModule> imagesModule and pass the parsed list to the constructor. Finally in your adapter onBindViewHolder() change the picasso code to

Picasso.with(context).load(imageUrls.get(position)).into(holder.appImage);

hope it helps