如何从服务器响应中提取适配器RecyclerView的对象列表?

问题描述 投票:0回答:1

收到服务器的答复。根据我创建的模型分配对象。但我不知道如何把它拉出来。在下面的图片中,我需要提取“结果”并将其添加到适配器RecyclerView中

this picture shows the work of the Retrofit2

我尝试创建一个新列表并使用该方法.addAll(response.body)

public class SearchResultOfCourses extends Fragment {
List<UserModelDto> mUserModelDtoList;
List<UserModelDto> mResults;
private RecyclerView mRecyclerView;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View qView = inflater.inflate(R.layout.search_result_of_courses, container, false);
    mRecyclerView = qView.findViewById(R.id.list);
    mUserModelDtoList = new ArrayList<>();
    DataAdapter mDataAdapter = new DataAdapter(getContext(), mUserModelDtoList);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.setAdapter(mDataAdapter);
    return qView;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mResults = new ArrayList<>();
    NetworkService.getInstance().getJSONApi().getResult(1,
            12,
            "java",
            "price-paid",
            true,
            "en",
            "beginner",
            "highest-rated",
            4).enqueue(new Callback<UserModelDto>() {
        @Override
        public void onResponse(Call<UserModelDto> call, Response<UserModelDto> response) {
            response.body();
        }

        @Override
        public void onFailure(Call<UserModelDto> call, Throwable t) {

        }
    });

}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
}
}

我的POJO课程

public class UserModelDto {
@JsonProperty("count")
private int count;
@JsonProperty("next")
private String next;

@JsonProperty("previous")
private Object previous;

@JsonProperty("results")
private List<Result> results = null;
public List<Result> getResults() {
    return results;
}
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

public static class PriceDetail {

    @JsonProperty("currency")
    private String currency;
    @JsonProperty("currency_symbol")
    private String currencySymbol;
    @JsonProperty("price_string")
    private String priceString;

    public String getPriceString() {
        return priceString;
    }

    @JsonProperty("amount")
    private float amount;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonAnyGetter
    private Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    private void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}
public static class Result {

    @JsonProperty("_class")
    private String _class;
    @JsonProperty("id")
    private int id;
    @JsonProperty("title")
    private String title;
    @JsonProperty("url")
    private String url;
    @JsonProperty("is_paid")
    private boolean isPaid;
    @JsonProperty("price")
    private String price;
    @JsonProperty("price_detail")
    private PriceDetail priceDetail;
    @JsonProperty("visible_instructors")
    private List<VisibleInstructor> visibleInstructors = null;
    @JsonProperty("image_125_H")
    private String image125H;
    @JsonProperty("image_240x135")
    private String image240x135;
    @JsonProperty("is_practice_test_course")
    private boolean isPracticeTestCourse;
    @JsonProperty("image_480x270")
    private String image480x270;
    @JsonProperty("published_title")
    private String publishedTitle;
    @JsonProperty("predictive_score")
    private Object predictiveScore;

    public String get_class() {
        return _class;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getUrl() {
        return url;
    }

    public boolean isPaid() {
        return isPaid;
    }

    public String getPrice() {
        return price;
    }

    public PriceDetail getPriceDetail() {
        return priceDetail;
    }

    public List<VisibleInstructor> getVisibleInstructors() {
        return visibleInstructors;
    }

    public String getImage125H() {
        return image125H;
    }

    public String getImage240x135() {
        return image240x135;
    }

    public boolean isPracticeTestCourse() {
        return isPracticeTestCourse;
    }

    public String getImage480x270() {
        return image480x270;
    }

    @JsonProperty("relevancy_score")
    private float relevancyScore;
    @JsonProperty("input_features")
    private Object inputFeatures;
    @JsonProperty("lecture_search_result")
    private Object lectureSearchResult;
    @JsonProperty("curriculum_lectures")
    private List<Object> curriculumLectures = null;
    @JsonProperty("order_in_results")
    private Object orderInResults;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonAnyGetter
    private Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    private void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}
public static class VisibleInstructor {
    public String getTitle() {
        return title;
    }

    @JsonProperty("title")
    private String title;
    @JsonProperty("image_50x50")
    private String image50x50;
    @JsonProperty("job_title")
    private String jobTitle;
    @JsonProperty("url")
    private String url;
    @JsonProperty("image_100x100")
    private String image100x100;
    @JsonProperty("_class")
    private String _class;
    @JsonProperty("display_name")
    private String displayName;
    @JsonProperty("initials")
    private String initials;
    @JsonProperty("name")
    private String name;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

}

答:重要!内部类在数据传输对象(PO​​JO)中必须是静态的

 public void onResponse(Call<UserModelDto> call, Response<UserModelDto> response) {
            moreUserModel.addAll(**response.body().getResults()**);
            generateContent(moreUserModel);
        }
java android android-recyclerview retrofit2
1个回答
0
投票

尝试在适配器中创建addAllItems(List<UserModelDto>)方法,然后在其中分配项目并在之后调用notifyDataSetChanged();。看看是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.