当前位置: 首页 > 知识库问答 >
问题:

使用json-indexoutofboundsexception创建列表:无效索引0,大小为0

羿季
2023-03-14

我正在尝试从instagram api获取关于特定标记的json数据。从响应im将所有url保存到一个只有字符串url属性的自定义类映像。在instagram的每个回复中,它都有大约20个媒体文件,但我似乎无法构建我的图像列表来显示在我的回收器视图中

Response.enqueue(new Callback(){@override public void onResponse(Call Call,Response Response){if(Response.issucess()){

                mEditText.setText("secces");
                StringBuilder sb = new StringBuilder();
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));
                    String line;

                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }

                    JSONObject tagResponse = new JSONObject(sb.toString());

                    for (int i = 0; i < tagResponse.length(); i++) {
                        JSONObject pagination = tagResponse.getJSONObject("pagination");

                        mMaxId = pagination.getString("next_max_id");
                        mMinId= pagination.getString("next_min_id");

                        JSONObject meta = tagResponse.getJSONObject("meta");
                        JSONArray data = tagResponse.getJSONArray("data");

                        for (int j = 0; j < data.length(); j++) {

                            JSONArray tags = data.getJSONObject(j).getJSONArray("tags");


                            JSONObject images = data.getJSONObject(j).getJSONObject("images").getJSONObject("low_resolution");
                            mEditText.setText(images.getString("url"));

                            Picture picture = new Picture();
                            picture.setURL(images.getString("url"));

                            mAdapter.addImage(picture);

                        }
                    }
                    //displayResults(data);


                } catch (Exception e) {
                    e.printStackTrace();
                }

                //Log.d(TAG, "CallonResponse isSuccess " + sb.toString() + " ----- ");
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            //Log.d(TAG, "CallonResponse onFailure! " + t.getMessage());
            t.printStackTrace();
        }
    });

当我做以下几行的时候...

Picture picture = new Picture();
picture.setURL(images.getString("url"));
mAdapter.addImage(picture);

根据我的调试技巧,我可以得到images.getString(“URL”),我觉得图片的创建有一些问题,但这没有意义,因为所有的图片类都有一个属性,所以可能会有一个问题。下面是我的适配器的代码...

公共类ImageAdapter扩展RecyclerView.Adapter{

private List<Picture> mPictures;
public ImageAdapter(){
    mPictures = new ArrayList<>();
}

@Override
public Holder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.sample_layout, viewGroup, false);
    return new Holder(row);
}

@Override
public void onBindViewHolder(Holder holder, int i) {
    Picture currPic = mPictures.get(i);
    Picasso.with(holder.itemView.getContext()).load(currPic.getURL()).into(holder.mPhoto1);

}

@Override
public int getItemCount() {
    return 600;
}

public void addImage(Picture picture) {
    mPictures.add(picture);
}

public class Holder extends RecyclerView.ViewHolder{

    private ImageView mPhoto1, mPhoto2;
    public Holder(View itemView) {
        super(itemView);

        mPhoto1 = (ImageView)itemView.findViewById(R.id.image1);
        //mPhoto2 = (ImageView)itemView.findViewById(R.id.image2);
    }
}

}

private void configViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //mLayoutManager = new StaggeredGridLayoutManager(VR_SPAN_COUNT, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
//        mAdapter = new ImageAdapter();
//        mRecyclerView.setAdapter(mAdapter);
    }    

共有1个答案

孟英光
2023-03-14

问题是您使用空数组mpictures创建适配器。

您必须执行以下操作:

在您的活动/片段中,您必须创建图片列表,例如:

循环之后,必须创建适配器ImageAdapter mAdapter=new ImageAdapter(mPictures);并将其设置为回收器视图-mrecycerview.setadapter(mAdapter);

您还必须像这样更改适配器结构:

public ImageAdapter(List<Picture> pictures){
        mPictures = pictures;
}

适配器中的方法getItemCount()必须如下所示:

@Override
public int getItemCount() {
    return mPictures.size();
}
 类似资料: