我试图从instagram api获取有关特定标签的json数据。从响应中我将所有url保存到一个只有String url属性的自定义类Image。在每个Instagram响应中,它有大约20个媒体文件,但我似乎无法构建我的回收站视图中显示的图像列表
response.enqueue(new Callback(){ @覆盖 public void onResponse(来电,回应回复){ if(response.isSuccess()){
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(&#34; url&#34;) 我觉得在创建图片方面存在一些问题但是没有意义,因为所有图片类都有一个属性,所以addImage可能存在问题。下面我发布了适配器的代码...
公共类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);
}
答案 0 :(得分:1)
问题是您使用空数组mPictures创建适配器。
您必须执行以下操作:
在您的活动/片段中,您必须创建图片列表,例如:
List<Picture> mPictures = new ArrayList<Picture>;
当您迭代您的响应(数据)时,您必须使用
mPictures.add(picture);
代替mAdapter.addImage(picture);
循环后,您必须创建适配器ImageAdapter mAdapter = new ImageAdapter(mPictures);
并将其设置为Recycler视图 - mRecyclerView.setAdapter(mAdapter);
。
此外,你必须改变你的适配器构造:
public ImageAdapter(List<Picture> pictures){
mPictures = pictures;
}
适配器中的方法getItemCount()必须如下所示:
@Override
public int getItemCount() {
return mPictures.size();
}
适配器中的方法addImage()不再需要了。就是这样。
答案 1 :(得分:0)
由于您拥有硬编码的600项,因此recyclerview希望显示项目并询问第一个元素(0)
Picture currPic = mPictures.get(i);
然后抛出异常,因为你没有图片。
@Override
public int getItemCount() {
return mPictures.size();
}
和
mAdapter.addImage(picture);
recyclerView.notifyItemInserted(adapter.getItemCount()-1)
应该做正确的事