在RecyclerView中的一个Json对象中填充两个Json Array项

时间:2016-04-29 08:39:51

标签: android arrays json android-recyclerview

我从服务器获取一个Json对象,并且Json对象包含两个Json数组。我知道如何将数据放入json数组中的recyclerview。但我不知道如何将两个json数组数据放入recyclerview。另外还有一件事,One Json数组包含 rep,name,name1 。此处名称表示 name1的ID(代码)。下一个数组包含客户(代码) coll 。此处名称客户是相同的。我从两个不同的表生成json。我现在成功填充了recyclerview中的 name1

我想要的是获取 coll 并将其与 name1 放在一起。例如,每行需要表示 name1 name1的coll 值name1的对应coll值。某些 name1 没有 coll 值(null)。在这种情况下,coll文本视图不需要显示任何内容。所以两个数组大小不相等。

这是我的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 个答案:

没有答案