如何将带有GSON数据的嵌套JSON列表传递给Recycler View

时间:2017-07-29 03:43:48

标签: java android json android-recyclerview gson

POJO ParentClass-Model

public class EditorialsModel {

    @SerializedName("Editorial_Id")
    @Expose
    private String editorialId;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("content")
    @Expose
    private String content;
    @SerializedName("content_sample")
    @Expose
    private String contentSample;
    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("Words")
    @Expose
    private List<Word> words = null;

    public String getEditorialId() {
        return editorialId;
    }

    public void setEditorialId(String editorialId) {
        this.editorialId = editorialId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContentSample() {
        return contentSample;
    }

    public void setContentSample(String contentSample) {
        this.contentSample = contentSample;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public List<Word> getWords() {
        return words;
    }

    public void setWords(List<Word> words) {
        this.words = words;
    }

}

POJO ChildClass-Model

public class Word {

    @SerializedName("Word")
    @Expose
    private String word;
    @SerializedName("Meaning")
    @Expose
    private String meaning;
    @SerializedName("Synonym")
    @Expose
    private List<String> synonym = null;
    @SerializedName("Antonyms")
    @Expose
    private List<String> antonyms = null;

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getMeaning() {
        return meaning;
    }

    public void setMeaning(String meaning) {
        this.meaning = meaning;
    }

    public List<String> getSynonym() {
        return synonym;
    }

    public void setSynonym(List<String> synonym) {
        this.synonym = synonym;
    }

    public List<String> getAntonyms(int position) {
        return antonyms;
    }

    public void setAntonyms(List<String> antonyms) {
        this.antonyms = antonyms;
    }

}

我使用GSON解析了JSON中的数据

                        GsonBuilder gsonBuilder=new GsonBuilder();
                        Gson gson=gsonBuilder.create();
                        List<EditorialsModel> posts = new ArrayList<>();
                        //EditorialsModel[] users=gson.fromJson(response,EditorialsModel[].class);
                        posts = Arrays.asList(gson.fromJson(response, EditorialsModel[].class));
                        adapter = new RecyclerViewAdapter(MainActivity.this, posts);
                        recyclerView.setAdapter(adapter);

我已将帖子列表传递给回收站视图,但无法从Word类

访问getter方法

以下代码获取父方法工作正常

    holder.songTitle.setText("Id: " + itemList.get(position).getEditorialId());
    holder.songYear.setText("Title: " + itemList.get(position).getTitle());
    holder.songAuthor.setText("Content : " + itemList.get(position).getContentSample());

但无法得到这样的方法

holder.synonyms.setText(itemList.get(position).getWords().size());

0 个答案:

没有答案