2017-11-16 5 views
-3

出力は、リストの最後の項目のみを示し、間違ったデータを示して すなわち、 16:30 16:15-16:15-16 00-16:15 16それは 16を示すべきであるとして、30 : 15-16:30Recyclerviewが

JSON出力ここ

{ 
    "slots": [{ 
     "1": { 
      "slot": "16:00-16:15", 
      "value": "1", 
      "ar_index": "1" 
     }, 
     "2": { 
      "slot": "16:15-16:30", 
      "value": "2", 
      "ar_index": "2" 
     } 
    }] 
} 

アダプタ Imがこれら二つのスロットをフェッチし、recyclerviewしかしイムgetingで表示を使用して、JSON出力をフェッチするために使用しrecyclerviewで設定イムpostexecuteコードであります最後の時間のみリスト。コード

@Override 
     protected void onPostExecute(String result) { 

      //this method will be running on UI thread 

      pdLoading.dismiss(); 
      List<DataBook> data=new ArrayList<>(); 

      pdLoading.dismiss(); 
      try { 
        JSONObject jsonObject = new JSONObject(result); 
        System.out.println("jsonObject_1 = "+jsonObject); 

        if(jsonObject != null){ 
         JSONArray jsonArray = jsonObject.getJSONArray("slots"); 
         System.out.println("jsonArray_2 = "+jsonArray); 

         if(jsonArray != null) { 
          for (int i = 0; i < jsonArray.length(); i++) { 
           System.out.println("jsonArray.length() = "+jsonArray.length()); 
           System.out.println("i_value = "+i); 

           JSONObject elem = jsonArray.getJSONObject(i); 
           System.out.println("elem = " + elem); 

           for (int j = 1; j <= elem.length(); j++) { 
            System.out.println("elem.length() = "+elem.length()); 
            System.out.println("j_value = "+j); 

            JSONObject elem1 = elem.getJSONObject(String.valueOf(j)); 
            System.out.println("elem1 = " + elem1); 
            DataBook fishData = new DataBook(); 

            if (elem1 != null) { 
             fishData.fishName = elem1.getString("slot"); 
             System.out.println("fishData.fishName = " + fishData.fishName); 
            } 
            data.add(fishData); 
           } 
          } 
         } 
        } 
// Setup and Handover data to recyclerview 
       mRVFishPrice = (RecyclerView)findViewById(R.id.rc_slotsList); 
       mAdapter = new AdapterFish(BookActivity.this, data); 
       mRVFishPrice.setAdapter(mAdapter); 
       mRVFishPrice.setLayoutManager(new LinearLayoutManager(BookActivity.this)); 

      } catch (JSONException e) { 
       Toast.makeText(BookActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 
      } 
     } 

の問題を理解することはできませんこれは私のアダプタクラスである私は、結果を設定していた

public class AdapterFish extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    private Context context; 
    private LayoutInflater inflater; 
    List<DataBook> data= Collections.emptyList(); 
    DataBook current; 
    int currentPos=0; 

    // create constructor to innitilize context and data sent from MainActivity 
    public AdapterFish(Context context, List<DataBook> data){ 
     this.context=context; 
     inflater= LayoutInflater.from(context); 
     this.data=data; 
    } 

    // Inflate the layout when viewholder created 
    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view=inflater.inflate(R.layout.book_container_fish, parent,false); 
     MyHolder holder=new MyHolder(view); 
     return holder; 
    } 

    // Bind data 
    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

     // Get current position of item in recyclerview to bind data and assign values from list 
     MyHolder myHolder= (MyHolder) holder; 
     current = data.get(position); 

     myHolder.textFishName.setText(current.fishName); 
     System.out.println("holder_current.fishName"+current.fishName); 

    } 

    // return total item from List 
    @Override 
    public int getItemCount() { 
     return data.size(); 
    } 

    class MyHolder extends RecyclerView.ViewHolder{ 

     TextView textFishName; 
     ImageView ivFish; 
     TextView textSize; 
     TextView textType; 
     TextView textPrice; 

     // create constructor to get widget reference 
     public MyHolder(View itemView) { 
      super(itemView); 
      textFishName= (TextView) itemView.findViewById(R.id.textFishName); 
     } 

    } 

} 
+0

int型の位置は、あなたがポジションを開始する必要がありますので、あなたに値1を与えます - 1; –

+0

位置1のアプリケーションを追加するとエラーが発生してクラッシュしました java.lang.ArrayIndexOutOfBoundsException:length = 10; index = -1 –

+0

public AdapterFish(コンテキストコンテキスト、リストデータ){ this.context = context; inflater = LayoutInflater.from(context); this.data = data; }あなたは、あなたが持っているすべてのデータを印刷します。あなたは問題を抱えてくるでしょう。 –

答えて

0
public AdapterFish(Context context, List<DataBook> data){ 
     this.context=context; 
     inflater= LayoutInflater.from(context); 
     this.data=data; 
     system.out.println(""+data.size()); 
//check how much data that you have got 


    } 
@Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

     // Get current position of item in recyclerview to bind data and 
     assign values from list 
     MyHolder myHolder= (MyHolder) holder; 
     current = data.get(position-1); 

     myHolder.textFishName.setText(current.fishName); 
     System.out.println("holder_current.fishName"+current.fishName); 

    } 
+0

位置-1のアプリケーションを追加するとエラーが発生しました。 java.lang.ArrayIndexOutOfBoundsException:length = 10;インデックス= -1 –

関連する問題