我試圖從instagram api獲取有關特定標記的json數據。從響應im保存所有的url只有一個String url屬性的自定義類圖像。在每一個Instagram的響應具有約20家媒體的文件,但我似乎無法構建圖像的名單在我的回收站視圖中顯示使用JSON創建列表 - IndexOutOfBoundsException:索引0無效,大小爲0
response.enqueue(新的回調(){ @覆蓋 公共無效onResponse(電話呼叫,響應響應){ 如果(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);
根據我的調試技巧我能get images.getString(「url」) 我覺得在創建圖片時存在一些問題,但沒有意義,因爲所有圖片類都有一個屬性,所以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);
}
@Override public int getItemCount(){ return 600; } 爲什麼它總是回到600? –
你說的沒錯,那只是我鬼混...我已經改變了,因爲其他建議感謝指出! –