Search code examples
androidandroid-layoutandroid-linearlayoutonclicklistener

Pass Array data on click to fragment


I'm trying to do the following. LinearLayout[i] is pressed JSONObject[i] should be sent to the Fragment.
But where ever i press only the last object in the JSONObject array is getting passed.

Any help is appreciated thanks.

LinearLayout demp[];
        JSONArray darray; //some data
        int arry_len = darray.length();
        demp = new LinearLayout[arry_len];
    JSONObject jso[] = new JSONObject[arry_len];


        for(int i=0;i<arry_len;i++)
        {
          demp[i] = new LinearLayout(dfm.getContext());
                    ViewGroup.LayoutParams mainlay_param = new ViewGroup.LayoutParams(dpToPx(310),ViewGroup.LayoutParams.WRAP_CONTENT);
                    LinearLayout.LayoutParams mainllay_layou = new LinearLayout.LayoutParams(mainlay_param);
                    demp[i].setMinimumHeight(dpToPx(150));


        demp[i].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle args = new Bundle();

                            args.putString("JSON",jso[i].toString());
                            FragmentManager fman = getActivity().getFragmentManager();
                            ActionOrder aord = new ActionOrder();
                            aord.setArguments(args);
                            aord.show(fman,"ordact");
                        }
                });


    }

Solution

  • i think you should save current index of each layout to get JSONObject array when layout clicked by using view method setTag(int i) and get the value by getTag()