0

I was using Nested Recycler View to show that forms. I need to show questions and their options in a RecyclerView. For that I have used a recycler View to show questions and then I have used a recyclerView inside adapter of the main adapter. So that I can show its options like radios, checkboxs or TextView? Here I have used MultiView Recycler View

Here is the JSON Response

{
   
    "fields": [
        {
            "id": 29,
            "field_type": "checkbox-group",
            "label": "Checkbox group",
            "subtype": null,
            "required": false,
            "values": [
                {
                    "id": 39,
                    "form_field": 29,
                    "label": "Option 1",
                    "value": "option-1"
                },
                 {
                    "id": 40,
                    "form_field": 29,
                    "label": "Option 2",
                    "value": "option-2"
                },
                 {
                    "id": 41,
                    "form_field": 29,
                    "label": "Option 3",
                    "value": "option-3"
                }
                ,
                 {
                    "id": 42,
                    "form_field": 29,
                    "label": "Option 4",
                    "value": "option-4"
                }
            ]
        },
        {
            "id": 30,
            "field_type": "radio-group",
            "label": "Radio group",
            "subtype": null,
            "required": false,
            "values": [
                {
                    "id": 40,
                    "form_field": 30,
                    "label": "Option 1",
                    "value": "option-1"
                },
                {
                    "id": 41,
                    "form_field": 30,
                    "label": "Option 2",
                    "value": "option-2"
                },
                {
                    "id": 42,
                    "form_field": 30,
                    "label": "Option 3",
                    "value": "option-3"
                }
            ]
        },
        {
            "id": 31,
            "field_type": "text",
            "label": "Text field",
            "subtype": "text",
            "required": false,
            "values": []
        }
    ]
}

Here is the XML Code for main Recycler View

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Form.SurveyFormMe">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycQues"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Submit" />
</androidx.appcompat.widget.LinearLayoutCompat>

** Here is Activity And All Adapters **

public class SurveyFormMe extends AppCompatActivity {

    ActivitySurveyFormMeBinding binding;
    List<ModelData> modelData = new ArrayList<>();
    String Questionaires = "{\n" +
            "    \"survey\": {\n" +
            "        \"heading\": \"TEST SURVEY\",\n" +
            "        \"description\": \"Aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa\",\n" +
            "        \"location\": \"Agra, Uttar Pradesh, India\",\n" +
            "        \"latitude\": 27.1766701,\n" +
            "        \"longitude\": 78.00807449999999,\n" +
            "        \"location_bound_radius\": 100.0,\n" +
            "        \"active\": true,\n" +
            "        \"blocked\": false\n" +
            "    },\n" +
            "    \"medias\": [\n" +
            "        {\n" +
            "            \"id\": 1,\n" +
            "            \"media\": \"/media/survey_media/audi_r8_anaglyph_3d_car_74529_1680x1050.jpg\",\n" +
            "            \"media_type\": \".jpg\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 2,\n" +
            "            \"media\": \"/media/survey_media/Ye_Dil_Tum_Bin.mp4\",\n" +
            "            \"media_type\": \".mp4\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"flag\": true,\n" +
            "    \"card_id\": \"2778\",\n" +
            "    \"link_id\": \"18536327-7e97-5859-8ec3-52e2f46bccb6\",\n" +
            "    \"fields\": [\n" +
            "        {\n" +
            "            \"id\": 29,\n" +
            "            \"field_type\": \"checkbox-group\",\n" +
            "            \"label\": \"Checkbox group\",\n" +
            "            \"subtype\": null,\n" +
            "            \"required\": false,\n" +
            "            \"values\": [\n" +
            "                {\n" +
            "                    \"id\": 39,\n" +
            "                    \"form_field\": 29,\n" +
            "                    \"label\": \"Option 1\",\n" +
            "                    \"value\": \"option-1\"\n" +
            "                },\n" +
            "\t\t\t\t {\n" +
            "                    \"id\": 40,\n" +
            "                    \"form_field\": 29,\n" +
            "                    \"label\": \"Option 2\",\n" +
            "                    \"value\": \"option-2\"\n" +
            "                },\n" +
            "\t\t\t\t {\n" +
            "                    \"id\": 41,\n" +
            "                    \"form_field\": 29,\n" +
            "                    \"label\": \"Option 3\",\n" +
            "                    \"value\": \"option-3\"\n" +
            "                }\n" +
            "\t\t\t\t,\n" +
            "\t\t\t\t {\n" +
            "                    \"id\": 42,\n" +
            "                    \"form_field\": 29,\n" +
            "                    \"label\": \"Option 4\",\n" +
            "                    \"value\": \"option-4\"\n" +
            "                }\n" +
            "            ]\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 30,\n" +
            "            \"field_type\": \"radio-group\",\n" +
            "            \"label\": \"Radio group\",\n" +
            "            \"subtype\": null,\n" +
            "            \"required\": false,\n" +
            "            \"values\": [\n" +
            "                {\n" +
            "                    \"id\": 40,\n" +
            "                    \"form_field\": 30,\n" +
            "                    \"label\": \"Option 1\",\n" +
            "                    \"value\": \"option-1\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"id\": 41,\n" +
            "                    \"form_field\": 30,\n" +
            "                    \"label\": \"Option 2\",\n" +
            "                    \"value\": \"option-2\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"id\": 42,\n" +
            "                    \"form_field\": 30,\n" +
            "                    \"label\": \"Option 3\",\n" +
            "                    \"value\": \"option-3\"\n" +
            "                }\n" +
            "            ]\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 31,\n" +
            "            \"field_type\": \"text\",\n" +
            "            \"label\": \"Text field\",\n" +
            "            \"subtype\": \"text\",\n" +
            "            \"required\": false,\n" +
            "            \"values\": []\n" +
            "        }\n" +
            "    ]\n" +
            "}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySurveyFormMeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.recycQues.setHasFixedSize(true);
        binding.recycQues.setLayoutManager(new LinearLayoutManager(this));

        try {
            JSONObject jsonObject = new JSONObject(Questionaires);
            JSONArray jsonArray = jsonObject.getJSONArray("fields");
            if (jsonArray.length() > 0) {
                List<ModelValues> modelValues = null;
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    String fieldType = jsonObject1.getString("field_type");
                    String lable = jsonObject1.getString("label");
                    JSONArray jsonArray1 = jsonObject1.getJSONArray("values");
                    if (jsonArray1.length() > 0) {
                        modelValues = new ArrayList<>();
                        for (int j = 0; j < jsonArray1.length(); j++) {
                            JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
                            String id = jsonObject2.getString("id");
                            String form_field = jsonObject2.getString("form_field");
                            String label = jsonObject2.getString("label");
                            String value = jsonObject2.getString("value");
                            modelValues.add(new ModelValues(id, form_field, label, value));
                        }
                    } else {

                    }
                    modelData.add(new ModelData(lable, fieldType, modelValues));
                }
                setMainCategoryRecycler(modelData);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        binding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    public class AdapterAllList extends RecyclerView.Adapter {
        public static final String MSG_TYPE_RADIO = "radio-group";
        public static final String MSG_TYPE_CHECKBOX = "checkbox-group";
        public static final String MSG_TYPE_INPUTBOX = "text";
        List<ModelRadioVal> modelRadioVals = new ArrayList<>();
        List<ModelCheckVal> modelCheckVals = new ArrayList<>();
        Context context;
        List<ModelData> modelMessages;

        public AdapterAllList(Context context, List<ModelData> modelMessages) {
            this.context = context;
            this.modelMessages = modelMessages;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view;
            if (viewType == 0) {
                view = layoutInflater.inflate(R.layout.radio_btn_question_list, parent, false);
                return new ViewHolderRadio(view);
            }
            if (viewType == 1) {
                view = layoutInflater.inflate(R.layout.check_box_recycler_view_item, parent, false);
                return new ViewHolderCheckBox(view);
            }
            view = layoutInflater.inflate(R.layout.input_box_layout, parent, false);
            return new ViewHolderInputBox(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (modelMessages.get(position).getId().equals(MSG_TYPE_RADIO)) {
                ViewHolderRadio holderRadio = (ViewHolderRadio) holder;
                holderRadio.questionTwo.setText(modelMessages.get(position).getTitle());
                setRadioReclerView(holderRadio.recyRadioList, modelMessages.get(position).getCategoryItemList());
            } else if (modelMessages.get(position).getId().equals(MSG_TYPE_CHECKBOX)) {
                ViewHolderCheckBox holderCheck = (ViewHolderCheckBox) holder;
                holderCheck.question.setText(modelMessages.get(position).getTitle());
                setCheck(holderCheck.checkBoxRec, modelMessages.get(position).getCategoryItemList());
            } else {
                ViewHolderInputBox holderInput = (ViewHolderInputBox) holder;
                holderInput.quest.setText(modelMessages.get(position).getTitle());
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (modelMessages.get(position).getId().equals(MSG_TYPE_RADIO)) {//Radio
                return 0;
            }
            if (modelMessages.get(position).getId().equals(MSG_TYPE_CHECKBOX)) {//CheckBox
                return 1;
            }
            return 2;
        }

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

        class ViewHolderRadio extends RecyclerView.ViewHolder {
            TextView questionTwo;
            RecyclerView recyRadioList;

            public ViewHolderRadio(@NonNull View itemView) {
                super(itemView);
                questionTwo = itemView.findViewById(R.id.questionTwo);
                recyRadioList = itemView.findViewById(R.id.recyRadioList);
            }
        }

        class ViewHolderCheckBox extends RecyclerView.ViewHolder {
            TextView question;
            RecyclerView checkBoxRec;

            public ViewHolderCheckBox(@NonNull View itemView) {
                super(itemView);
                question = itemView.findViewById(R.id.question);
                checkBoxRec = itemView.findViewById(R.id.checkBoxRec);
            }
        }

        class ViewHolderInputBox extends RecyclerView.ViewHolder {
            TextView quest;

            public ViewHolderInputBox(@NonNull View itemView) {
                super(itemView);
                quest = itemView.findViewById(R.id.quest);
            }
        }

        private void setRadioReclerView(RecyclerView recyclerView, List<ModelValues> categoryItemList) {
            for (int i = 0; i < categoryItemList.size(); i++) {
                ModelRadioVal modelRadioVal = new ModelRadioVal();
                modelRadioVal.setId(categoryItemList.get(i).getId());
                modelRadioVal.setLabel(categoryItemList.get(i).getLabel());
                Log.d("werty", "setRadioReclerView: " + categoryItemList.get(i).getLabel());
                modelRadioVal.setForm_field(categoryItemList.get(i).getForm_field());
                modelRadioVal.setValue(categoryItemList.get(i).getValue());
                modelRadioVals.add(modelRadioVal);
            }
            Log.d("asdfghjhfds", "setRadioReclerView: " + modelRadioVals.toString());
            CategoryItemRecyclerAdapter itemRecyclerAdapter = new CategoryItemRecyclerAdapter(context, modelRadioVals);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(itemRecyclerAdapter);
        }

        private void setCheck(RecyclerView recyclerView, List<ModelValues> categoryItemList) {

            for (int i = 0; i < categoryItemList.size(); i++) {
                ModelCheckVal modelCheckVal = new ModelCheckVal();
                modelCheckVal.id = categoryItemList.get(i).getId();
                modelCheckVal.label = categoryItemList.get(i).getLabel();
                Log.d("werty", "setRadioReclerView: " + categoryItemList.get(i).getLabel());
                modelCheckVal.form_field = categoryItemList.get(i).getForm_field();
                modelCheckVal.value = categoryItemList.get(i).getValue();
                modelCheckVals.add(modelCheckVal);
            }
            AdapterCheckItem itemRecyclerAdapter = new AdapterCheckItem(context, modelCheckVals);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(itemRecyclerAdapter);
        }
    }

    public class CategoryItemRecyclerAdapter extends RecyclerView.Adapter<CategoryItemRecyclerAdapter.ExViewHolder> {

        Context context;
        private List<ModelRadioVal> categoryItemList;
        private int checkedPosition = -1; //-1: no default selection  or 0 for first option selected

        public CategoryItemRecyclerAdapter(Context context, List<ModelRadioVal> categoryItemList) {
            this.context = context;
            this.categoryItemList = categoryItemList;
        }

        @NonNull
        @Override
        public ExViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new ExViewHolder(LayoutInflater.from(context).inflate(R.layout.raj_item_radio, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ExViewHolder exViewHolder, int i) {
            exViewHolder.bind(categoryItemList.get(i));

        }

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

        public class ExViewHolder extends RecyclerView.ViewHolder {
            TextView radio1;
            ImageView check;

            public ExViewHolder(@NonNull View itemView) {
                super(itemView);
                radio1 = itemView.findViewById(R.id.radio1);
                check = itemView.findViewById(R.id.iView);
            }

            void bind(final ModelRadioVal employee) {
                if (checkedPosition == -1) {
                    check.setVisibility(View.INVISIBLE);
                } else {
                    if (checkedPosition == getAdapterPosition()) {
                        check.setVisibility(View.VISIBLE);

                    } else {
                        check.setVisibility(View.INVISIBLE);
                    }
                }
                radio1.setText(employee.getLabel());

                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        check.setVisibility(View.VISIBLE);
                        if (checkedPosition != getAdapterPosition()) {
                            notifyItemChanged(checkedPosition);
                            checkedPosition = getAdapterPosition();

                        }
                    }
                });

            }
        }

        public ModelRadioVal getSelected() {
            if (checkedPosition != -1) {
                return categoryItemList.get(checkedPosition);
            }
            return null;
        }
    }

    private void setMainCategoryRecycler(List<ModelData> allCategoryList) {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        binding.recycQues.setLayoutManager(layoutManager);
        AdapterAllList adapterAllList = new AdapterAllList(this, allCategoryList);
        binding.recycQues.setAdapter(adapterAllList);
    }

    public class AdapterCheckItem extends RecyclerView.Adapter<AdapterCheckItem.ExViewHolder> {
        Context context;
        private List<ModelCheckVal> categoryItemList;

        public AdapterCheckItem(Context context, List<ModelCheckVal> categoryItemList) {
            this.context = context;
            this.categoryItemList = categoryItemList;
        }


        @NonNull
        @Override
        public ExViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new ExViewHolder(LayoutInflater.from(context).inflate(R.layout.raj_item_check, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ExViewHolder exViewHolder, int i) {
            exViewHolder.bindTvShow(categoryItemList.get(i));
            setScaleAnimation(exViewHolder.itemView);
        }

        private void setScaleAnimation(View itemView) {
            ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setDuration(1000);
            itemView.startAnimation(anim);
        }

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

        public List<ModelCheckVal> getSellectedTvShows() {
            List<ModelCheckVal> selectedTvShows = new ArrayList<>();
            for (ModelCheckVal tvShowModel : categoryItemList) {
                if (tvShowModel.isSelected) {
                    selectedTvShows.add(tvShowModel);
                }
            }
            return selectedTvShows;
        }
        public class ExViewHolder extends RecyclerView.ViewHolder {
            TextView radio1;
            ImageView iView;

            public ExViewHolder(@NonNull View itemView) {
                super(itemView);
                radio1 = itemView.findViewById(R.id.radio1);
                iView = itemView.findViewById(R.id.iView);
            }

            public void bindTvShow(ModelCheckVal modelCheckVal) {
                radio1.setText(modelCheckVal.label);
                if (modelCheckVal.isSelected) {
                    iView.setVisibility(View.VISIBLE);
                } else {
                    iView.setVisibility(View.INVISIBLE);
                }
                radio1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (modelCheckVal.isSelected) {
                            iView.setVisibility(View.INVISIBLE);
                            modelCheckVal.isSelected = false;
                            if (getSellectedTvShows().size() == 0) {

                            }
                        } else {

                            iView.setVisibility(View.VISIBLE);
                            modelCheckVal.isSelected = true;

                        }
                    }
                });
            }
        }
    }
}

And Also the Model classes are here

public class ModelData {
    String title;
    String id;
    List<ModelValues> categoryItemList;

    public ModelData(String title, String id, List<ModelValues> categoryItemList) {
        this.title = title;
        this.id = id;
        this.categoryItemList = categoryItemList;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<ModelValues> getCategoryItemList() {
        return categoryItemList;
    }

    public void setCategoryItemList(List<ModelValues> categoryItemList) {
        this.categoryItemList = categoryItemList;
    }
}

All are working well but I'm unable to get selected data from each View's on Click of submit button.

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44

0 Answers0