2016-02-26 43 views
0

我試圖從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); 
    }  
+0

@Override public int getItemCount(){ return 600; } 爲什麼它總是回到600? –

+0

你說的沒錯,那只是我鬼混...我已經改變了,因爲其他建議感謝指出! –

回答

1

的問題是,你創建一個空的數組mPictures適配器。

你必須做到以下幾點:

在你的活動/片段你要創建的圖片列表,例如:

List<Picture> mPictures = new ArrayList<Picture>;

,當你重複你的迴應(數據)您必須使用 而不是mAdapter.addImage(picture);

循環後,您必須創建適配器ImageAdapter mAdapter = new ImageAdapter(mPictures);並將其設置爲回收站視圖 - mRecyclerView.setAdapter(mAdapter);

而且你必須改變你的適配器constuctor這樣的:

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

在適配器的方法getItemCount()必須看起來像:

@Override 
public int getItemCount() { 
    return mPictures.size(); 
} 

的方法addImage()的適配器不再需要了。就這樣。

+0

哇,你的方法更有意義,我現在意識到我並沒有創建數組。所以我現在就嘗試了,我仍然得到同樣的錯誤,但我仍然覺得我朝着正確的方向邁出了一步。此刻,我將mPictures放入我的適配器中,一旦它完成數據。 –

+0

此外,我真的很困惑recyclerview我認爲這可能是一個問題。我試着編輯並告訴你如何設置它。 –

+0

當我調試它時,我看到數組正在被填充。它的大小正在增加,以及它充滿了圖像。當我訪問各個元素並記錄它們時,我可以看到這些網址。 –

0

既然你有你的硬編碼600個項目,該recyclerview希望得到的物品顯示和詢問的第一個元素(0)

Picture currPic = mPictures.get(i); 

然後拋出異常,因爲你有沒有照片。

@Override 
public int getItemCount() { 
    return mPictures.size(); 
} 

mAdapter.addImage(picture); 
recyclerView.notifyItemInserted(adapter.getItemCount()-1) 

應該做的正確的事情

相關問題