使用JSON将对象放入回收站视图

我正在尝试使用JSON将图像插入回收站视图。似乎正在读取信息,但它从未停止过,最终使我遇到了内存不足的异常。跟踪显示“背景粘性并发标记清除GC”反复释放了大约2分钟,然后引发了错误。对于我来说,这里的真正错误尚不清楚,或者我会提供更多信息,如果需要,请提出问题以进行澄清。

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://dog.ceo/api/breeds/image/random/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        JsonAnimalApi jsonAnimalApi = retrofit.create(JsonAnimalApi.class);
        Call<Animal> call = jsonAnimalApi.getAnimals();
        System.out.println("Here");
        call.enqueue(new Callback<Animal>() {
            @Override
            public void onResponse(Call<Animal> call, Response<Animal> response) {
                System.out.println("HERE");
                if (!response.isSuccessful()) {
                    while (names.size() < 50) names.add(response.body().getPicture());
                    tvResponse.setText("Code: " + response.code());
                    return;
                }
public interface JsonAnimalApi {
    @GET("animals")
    Call<Animal> getAnimals();
}
public class Animal {
    private ImageView  picture;
    private String status;
    private String message;

    public ImageView getPicture() {
        return picture;
    }

    public String getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private ArrayList<ImageView> mAnimalNames;
    private OnNoteListener mOnNote;
    public RecyclerViewAdapter(ArrayList<ImageView> mAnimalNames, OnNoteListener mOnNote) {
        this.mAnimalNames = mAnimalNames;
        this.mOnNote = mOnNote;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
        ViewHolder holder = new ViewHolder(view, mOnNote);
        return holder;
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: ");
    }
    @Override
    public int getItemCount() {
        return mAnimalNames.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView animalName;
        OnNoteListener onNote;
        public ViewHolder(@NonNull View itemView, OnNoteListener onNote) {
            super(itemView);
            animalName = itemView.findViewById(R.id.animal_name);
            this.onNote = onNote;

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            mOnNote.onNoteClick(getAdapterPosition(), mAnimalNames);
        }
    }

    public interface OnNoteListener {
        void onNoteClick(int pos, ArrayList<ImageView> names);
    }
}
评论