0

サーバーからJsonオブジェクトを取得していて、そのJsonオブジェクトに2つのJson配列が含まれていました。私はjson配列の中でrecyclerviewにデータを入れる方法を知っています。しかし、私は2つのjson配列データをrecyclerviewに入れる方法を知らない。 1つのJson配列には、rep、name、name1が含まれていました。 nameはname1のid(codeID)を意味します。次の配列にはcustomer(codeID)とcoll(名前と顧客は同じです)が含まれています。私は2つの異なるテーブルを使用してjsonを生成しています。私は、recyclerviewのname1を現在正常に入力しています。IndexOutOfBoundsException 1つのjsonオブジェクトの2つのjson配列からデータを取り込もうとしたとき

私が望むのは、name1とそれに対応するcoll値を取得することです。たとえば、各行はname1とname1のcoll値を表す必要があります。 name1の中にはcoll値(null)がありません。その場合、coll textviewは空である必要があります。場合によっては、2つの配列サイズ(長さ)が等しくないことがあります。

これは私のJsonです。

{ 
    "feed": [ 
    { 
     "rep": "503", 
     "name": "30000216", 
     "name1": "GERLINE TYRE HOUSE (BRANCH)" 
    }, 
    { 
     "rep": "503", 
     "name": "30000217", 
     "name1": "*G.L.P.K.Liyanage(ISURU ENTERP" 
    }, 
    { 
     "rep": "503", 
     "name": "30000218", 
     "name1": "**Kamal Motor Stores" 
    }, 
    { 
     "rep": "503", 
     "name": "30000223", 
     "name1": "T.Thamotherempillai & Sons" 
    }, 
    { 
     "rep": "503", 
     "name": "30001037", 
     "name1": "Iynkarn Motors(Distributor)" 
    }, 
    { 
     "rep": "503", 
     "name": "30001074", 
     "name1": "Northern Province MPCS FED" 
    }, 
    { 
     "rep": "503", 
     "name": "30001131", 
     "name1": "Kishor Tyre House" 
    }, 
    { 
     "rep": "503", 
     "name": "45000012", 
     "name1": "R.G.M.Transporters" 
    } 
    ], 
    "feeddd": [ 
    { 
     "customer": "30000220", 
     "coll": "92820.00" 
    }, 
    { 
     "customer": "30000222", 
     "coll": "1784348.61" 
    }, 
    { 
     "customer": "30000224", 
     "coll": "2661442.57" 
    }, 
    { 
     "customer": "30000225", 
     "coll": "15580475.00" 
    }, 
    { 
     "customer": "30001302", 
     "coll": "698968.93" 
    } 
    ] 
} 

YourDealerListFragmentクラス

private void getJsonRequest() { 
     progressView.start(); 

     final SQLiteHandler sqLiteHandler = new SQLiteHandler(getActivity().getApplicationContext()); 
     Cursor cr = sqLiteHandler.getData(sqLiteHandler); 
     cr.moveToFirst(); 

     do { 
      repNo = cr.getString(0); 
     } while (cr.moveToNext()); 
     cr.close(); 

     CustomJsonObjectRequest request = new CustomJsonObjectRequest(Request.Method.POST, AppConfig.URL_JSON_DEALER_LIST, hashMap, new Response.Listener<JSONObject>() { 


      @Override 
      public void onResponse(JSONObject response) { 
       progressView.stop(); 

       try { 
        JSONObject jsonObject = new JSONObject(String.valueOf(response)); 
        if (jsonObject.names().get(0).equals("feed")) { 
         dealerList = parseJsonResponse(response); 
         dlAdapter.setDealertList(dealerList); 

         JSONArray arrayDealerColl = response.getJSONArray("feeddd"); 
         ArrayList<DealerCollection> dealerColList = new ArrayList<>(); 
         for (int i = 0; i < arrayDealerColl.length(); i++) { 
          JSONObject obj3 = arrayDealerColl.getJSONObject(i); 
          String dealerName = obj3.getString("customer"); 
          String dealerCollection = obj3.getString("coll"); 

          DealerCollection dealerColl = new DealerCollection(); 
          dealerColl.setDealerName(dealerName); 
          dealerColl.setDealerCollection(dealerCollection); 

          dealerColList.add(dealerColl); 
          dlAdapter.setDealerCollectionList(dealerColList); 

         } 

        } else { 
         Toast.makeText(getActivity(), "No Dealers Available", Toast.LENGTH_SHORT).show(); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

      } 
     }) { 
      protected Map<String, String> getParams() throws AuthFailureError { 
       hashMap = new HashMap<String, String>(); 
       hashMap.put("repNo", repNo); 
       return hashMap; 
      } 
     }; 
     requestQueue.add(request); 
     request.setRetryPolicy(new DefaultRetryPolicy(15 * 1000, 0, 
       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
    } 


    private ArrayList<SuggestGetSet> parseJsonResponse(JSONObject response) { 
     ArrayList<SuggestGetSet> groupList = new ArrayList<>(); 
     if (response != null || response.length() > 0) { 

      try { 

       JSONArray arrayDelaers = response.getJSONArray(KEY_FEED_NAME); 

       for (int i = 0; i < arrayDelaers.length(); i++) { 
        JSONObject currentObject = arrayDelaers.getJSONObject(i); 
        String rep = currentObject.getString(KEY_REP_ID); 
        String name = currentObject.getString(KEY_REP_NAME); 
        String dealerId = currentObject.getString(KEY_DEALER_ID); 

        SuggestGetSet delaers = new SuggestGetSet(); 
        delaers.setId(rep); 
        delaers.setName(name); 
        delaers.setDealerId(dealerId); 

        groupList.add(delaers); 

       } 
       //Toast.makeText(getApplicationContext(), productList.toString(), Toast.LENGTH_LONG).show(); 

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

     return groupList; 
    } 

マイアダプタクラス

public class DealerListAdapter extends RecyclerView.Adapter<DealerListAdapter.ViewHolderDealerList> { 
    private LayoutInflater layoutInflater; 
    public Context mcontext; 
    private List<SuggestGetSet> dealerArrayList; 
    private List<SuggestGetSet> originalDealerArrayList; 
    private List<DealerCollection> dealerCollectionArrayList; 
    Typeface type; 
    private static String selectedRepId, selectedDealerId, selectedDealerName; 

    public DealerListAdapter() { 
    } 

    public DealerListAdapter(Context context) { 
     layoutInflater = LayoutInflater.from(context); 
     type = Typeface.createFromAsset(context.getAssets(), "helvr.ttf"); 
    } 

    public static String getDealerName() { 
     return selectedDealerName; 
    } 

    public static String getDealerID() { 
     return selectedDealerId; 
    } 

    public static String getRepID() { 
     return selectedRepId; 
    } 

    public DealerListAdapter(ArrayList<SuggestGetSet> dList, Context context) { 
     this.mcontext = context; 
     layoutInflater = LayoutInflater.from(context); 
     this.dealerArrayList = new ArrayList<>(dList); 
     this.originalDealerArrayList = new ArrayList<>(dList); 
     type = Typeface.createFromAsset(context.getAssets(), "helvr.ttf"); 
    } 

    public void setDealertList(ArrayList<SuggestGetSet> dealerAList) { 
     this.dealerArrayList = dealerAList; 
     originalDealerArrayList = new ArrayList<>(dealerAList); 
     notifyItemRangeChanged(0, dealerArrayList.size()); 
    } 

    public void setDealerCollectionList(ArrayList<DealerCollection> dealerColList) { 
     this.dealerCollectionArrayList = dealerColList; 
     notifyItemRangeChanged(0, dealerCollectionArrayList.size()); 
    } 

    @Override 
    public ViewHolderDealerList onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = layoutInflater.inflate(R.layout.custom_dealer_list_layout, parent, false); 
     ViewHolderDealerList viewHolderDealerList = new ViewHolderDealerList(view); 
     return viewHolderDealerList; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolderDealerList holder, int position) { 
     final SuggestGetSet model = dealerArrayList.get(position); 
     holder.bind(model); 

     final int pos = position; 

     final DealerCollection model_two = dealerCollectionArrayList.get(position); 
     holder.bind_two(model_two); 

     holder.dealerName.setText(dealerArrayList.get(position).getName()); 

     holder.dealerName.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       selectedDealerId = dealerArrayList.get(pos).getDealerId(); 
       selectedRepId = dealerArrayList.get(pos).getId(); 
       selectedDealerName = dealerArrayList.get(pos).getName(); 
      } 
     }); 

     if (dealerArrayList.get(position).getDealerId() == dealerCollectionArrayList.get(position).getDealerName()) { 
      holder.dealerCol.setText(dealerCollectionArrayList.get(position).getDealerCollection()); 
     } 

    } 

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

    static class ViewHolderDealerList extends RecyclerView.ViewHolder { 
     private TextView dealerName, dealerCol; 

     public ViewHolderDealerList(View itemView) { 
      super(itemView); 
      dealerName = (TextView) itemView.findViewById(R.id.yourDelaerName); 
      dealerCol = (TextView) itemView.findViewById(R.id.txtDelaerCollection); 
     } 

     public void bind(SuggestGetSet model) { 
      dealerName.setText(model.getName()); 
     } 

     public void bind_two(DealerCollection model) { 
      dealerCol.setText(model.getDealerCollection()); 
     } 

    } 

    public void setFilter(List<SuggestGetSet> models) { 
     dealerArrayList = new ArrayList<>(); 
     dealerArrayList.addAll(models); 
     notifyDataSetChanged(); 
    } 

} 

マイLogCat

04-29 14:06:53.529 11709-11709/com.ceatkelanisrilanka.dushanmadushanka.ceat E/AndroidRuntime: FATAL EXCEPTION: main 
java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10 
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 
    at java.util.ArrayList.get(ArrayList.java:304) 
    at com.ceatkelanisrilanka.dushanmadushanka.ceat.adapters.DealerListAdapter.onBindViewHolder(DealerListAdapter.java:83) 
    at com.ceatkelanisrilanka.dushanmadushanka.ceat.adapters.DealerListAdapter.onBindViewHolder(DealerListAdapter.java:21) 
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471) 
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) 
    at android.support.v7.widget.LayoutState.next(LayoutState.java:100) 
    at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1538) 
    at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:657) 
    at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:592) 
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) 
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) 
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) 
    at android.view.View.layout(View.java:13754) 
    at android.view.ViewGroup.layout(ViewGroup.java:4362) 
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649) 
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507) 
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420) 
    at android.view.View.layout(View.java:13754) 
    at android.view.ViewGroup.layout(ViewGroup.java:4362) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
    at android.view.View.layout(View.java:13754) 
    at android.view.ViewGroup.layout(ViewGroup.java:4362) 
    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695) 
    at android.view.View.layout(View.java:13754) 
    at android.view.ViewGroup.layout(ViewGroup.java:4362) 
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649) 
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507) 
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420) 
    at android.view.View.layout(View.java:13754) 
    at android.view.ViewGroup.layout(ViewGroup.java:4362) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
    at android.view.View.layout(View.java:13754) 
    at android.view.ViewGroup.layout(ViewGroup.java:4362) 
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649) 
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507) 
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420) 
    at android.view.View.layout(View.java:13754) 
    at android.view.ViewGroup.layout(ViewGroup.java:4362) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
    at android.view.View.layout(View.java:13754) 
    at android.view.ViewGroup.layout(ViewGroup.java:4362) 
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
    at android.view.Choreographer.doFrame(Choreographer.java:525) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 
    at android.os.Handler.handleCallback(Handler.java:615) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4745) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.intern 
+0

このバグはまだ23.1.1で固定されていませんが、一般的な回避策はexception.refer http://stackoverflow.com/questions/26827222/how-to-change-contents-ofをキャッチするだろう-recyclerview-while-scroll – sasikumar

+0

2番目の配列リスト項目をrecyclerviewの対応する名前に追加する方法を提案できますか? –

+0

はdealerCollectionArrayListオブジェクトサイズはdealerArrayListオブジェクトサイズと同じです...? –

答えて

0

この条件を書く代わりに、インデックス位置からオブジェクトを取得してください:

if (dealerCollectionArrayList.size < position) { 
     final DealerCollection model_two = dealerCollectionArrayList.get(position); 
     holder.bind_two(model_two); 

     if (dealerArrayList.get(position).getDealerId() == dealerCollectionArrayList.get(position).getDealerName()) { 
      holder.dealerCol.setText(dealerCollectionArrayList.get(position).getDealerCollection()); 
     } 
    }else { 
     //Set not available or something 
    } 
関連する問題