Home » Android » android – How can i get data from model file?-Exceptionshub

android – How can i get data from model file?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have this model files which I use to get data of Vimeo videos using Retrofit and Gson.

It’s all working fine, I get title, description and other things with no problem but I can’t get the thumbnail.

This is my adapter

class YoutubeHolder extends RecyclerView.ViewHolder {

        ImageView thumbnail;
        TextView title, description, publishedAt;

        YoutubeHolder(@NonNull View itemView) {
            super(itemView);
            thumbnail = itemView.findViewById(R.id.ImageThumb);
            title = itemView.findViewById(R.id.textViewTitle);
            description = itemView.findViewById(R.id.textViewDes);
            publishedAt = itemView.findViewById(R.id.textViewDate);
        }

        public void setData(VimeoVideo data) {
            String getTitle = data.getName();
            String getDescription = data.getDescription();
            String getPublishedAt = data.getCreated_time();

            // I want get it like this
            String getThumbnail = data.getPictures().getSizes().getLink;

            title.setText(getTitle);
            description.setText(getDescription);
            publishedAt.setText(getPublishedAt);

        }

    }

And this is my models

public class VimeoPictures {
    @SerializedName("sizes")
    @Expose
    private List<VimeoSizes> sizes;

    public VimeoPictures() {
    }

    public VimeoPictures(List<VimeoSizes> sizes) {
        this.sizes = sizes;
    }

    public List<VimeoSizes> getSizes() {
        return sizes;
    }

    public void setSizes(List<VimeoSizes> sizes) {
        this.sizes = sizes;
    }
}

And one for sizes

@SerializedName("width")
    @Expose
    private String width;

    @SerializedName("height")
    @Expose
    private String height;

    @SerializedName("link")
    @Expose
    private String link;

    ... Getters and Setters,  etc...

I searched for an answer but no success 🙁

How to&Answers:

I think the problem is in this line :

String getThumbnail = data.getPictures().getSizes().getLink;

when you call getSizes(), it return list of object. You cannot call getLink from a list. You need to get a certain element from the list.

As an example if you want to get the link of the first element in the list, you can do this :

String getThumbnail = data.getPictures().getSizes().get(0).getLink;