moayed ayasaa moayed ayasaa - 2 months ago 17
JSON Question

Error with Picasso library in

i tried to display ArrayList in Picasso with Recyclerview but i have the problem with Picasso how i can load the data from Array list to the Picasso?

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.getAllimage()).into(holder.appImage));
}
@Override
public int getItemCount() {
return imagesModules.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView appImage;
public ViewHolder(View itemView) {
super( itemView );
appImage = (ImageView) itemView.findViewById( R.id.appimages);
}}


and this is the module

public class ImagesModule

{
public List<String> getAllimage() {
return Allimage;}
public void setAllimage(List<String> allimage) {
Allimage = allimage;}
List<String> Allimage = new ArrayList<String>();
}


and here i get the json and add it to the Arraylist

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");
List<String> allimage = new ArrayList<String>();


for (int j=0; i<imageArray.length(); j++) {
JSONObject actor = imageArray.getJSONObject(i);
String name = actor.getString("label");
allimage.add(name);
imagesModule.setAllimage(allimage);
}

Answer

You need to get one image URL to load into Picasso, you cannot load a List.

Also, this class is basically a List, so just treat it as one.

public class ImagesModule extends ArrayList<String> {

}

Parsing - Two options, one module for all the images, or somehow deal with one module per entry datapoint

// TODO: Determine if you want one 'master' module for all the images
ImagesModule module = new ImagesModule(); 

JSONArray jsonArray = response.getJSONObject("feed").getJSONArray("entry");
for (int i = 0; i < jsonArray.length(); i++) {
    // TODO: Determine if you want one module 'per entry'
    /*** ImagesModule module = new ImagesModule(); ***/
    JSONArray imageArray = jsonArray.getJSONObject(i).getJSONArray("im:image");

    for (int j=0; i<imageArray.length(); j++) {
        JSONObject actor = imageArray.getJSONObject(i);
        String name = actor.getString("label");
        module.add(name); // TODO: Actually get an image, not an actor probably...
    }

Now, you can load that String into Picasso

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final ImagesModule imagesModule = imagesModules.get( position );

    final String img = imagesModule.get(position);
    Picasso.with(context).load(img).into(holder.appImage));
}
Comments