Save Radiobuttom after selection

Asked

Viewed 89 times

0

Hello, someone could help me in this matter, I have a list with questions in them there are 3 Radiosbuttom for responses, what I want is when the user click the save button, he lists the selected radios and save in a list, the way I managed to do it takes the Radios at the user’s click generating a problem, if the user clicks on another radio of the same position of the list he is saving the two radios that were clicked and keeping in the list that I later picked up and saved in Sqlite.

Follow the layout image.

inserir a descrição da imagem aqui

Follow the Adapter code.

public class ExpandableRecyclerAdapter extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
    private List<Repo> repos;
    public SparseBooleanArray expandState = new SparseBooleanArray();
    public ArrayList<String> listTxtRadio = new ArrayList<>();
    public ArrayList<String> listText = new ArrayList<>();
    public ArrayList<Integer> listIDRadio = new ArrayList<Integer>();
    public ArrayList<Integer> listId = new ArrayList<Integer>();
    public Context context;

    public RadioButton radioButton;

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    public ExpandableRecyclerAdapter(List<Repo> repos, Context context) {
        this.context = context;
        this.repos = repos;
        for (int i = 0; i < repos.size(); i++) {
            expandState.append(i, false);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        this.context = viewGroup.getContext();
        if (i == TYPE_HEADER) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.header_layout, viewGroup, false);
            return new HeaderVh(view);
        } else if (i == TYPE_ITEM) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_report_list, viewGroup, false);
            return new ItemVh(view);
        }
        throw new RuntimeException("No macth for" + i + ".");
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
        //Header
        final Repo repo = repos.get(position);

        if (viewHolder instanceof HeaderVh) {
            ((HeaderVh) viewHolder).headerTitle.setText(repo.getTitle());
        } else if (viewHolder instanceof ItemVh) {
            ((ItemVh) viewHolder).tvTitleList.setText(repo.getTitle());

            viewHolder.setIsRecyclable(false);
            viewHolder.tvTitleList.setText(repos.get(position).getTitle());
            viewHolder.tvTextList.setText(repos.get(position).getText());

            final boolean isExpanded = expandState.get(position);
            viewHolder.expandableLayout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);

            viewHolder.buttonLayoutArrow.setRotation(expandState.get(position) ? 180f : 0f);
            viewHolder.buttonLayoutArrow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    onClickButton(viewHolder.expandableLayout, viewHolder.buttonLayoutArrow, position);
                }
            });

            //Test onClick RadioButton
            viewHolder.mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {

                    int selectedRadioButtonID = viewHolder.mRadioGroup.getCheckedRadioButtonId();

                    //Test Salve in a ArrayList RadioButton Selected
                    radioButton = group.findViewById(selectedRadioButtonID);

                    String selectedText = (String) viewHolder.tvTitleList.getText();
                    String selectedRadioButtonText = radioButton.getText().toString();
                    int selectedRadioId = radioButton.getId();

                    listIDRadio.add(selectedRadioId);
                    listId.add(position);
                    listText.add(selectedText);
                    listTxtRadio.add(selectedRadioButtonText);

                    Log.i("log", "Item: " + listTxtRadio + " listRadio ");
                    Log.i("log", "Item: " + listId + " selectedIDList ");
                    Log.i("log", "Item: " + listIDRadio + " selectedIDRadio ");
                    Log.i("log", "Item: " + listText + " listText ");

                    //Del duplicate list
                            /*HashSet<String> hashSet = new HashSet<String>(listText);
                            hashSet.addAll(listText);
                            listText.clear();
                            listText.addAll(hashSet);
                            repo.setCheckList(listText);
                            Log.position("log", "Item: " + repo.getCheckList() + " getCheckList ");*/

                            /*if (checkedId == R.id.radio_conform) {
                                viewHolder.mRadioButtonConform.setChecked(true);
                                group.setTag(checkedId);
                                //Log.position("log", "Item: " + listConformed + " listConformed ");

                            } else if (checkedId == R.id.radio_not_applicable) {
                                viewHolder.mRadioButtonNotApplicable.setChecked(true);
                                group.setTag(checkedId);
                                //Log.position("log", "Item: " + listNotConformed + " listNotConformed ");

                            } else if (checkedId == R.id.radio_not_conform) {
                                viewHolder.mRadioButtonNotConform.setChecked(true);
                                group.setTag(checkedId);
                                //Log.position("log", "Item: " + listNotApplicable + " listNotApplicable ");

                            } else {
                                group.clearCheck();
                            }*/
                }
            });
            // End RadioButton
        }
        Log.i(TAG, "onBindViewHolder invoked" + position);
    }

    public int getItemViewType(int i) {
        if (isPositionHeader(i))
            return TYPE_HEADER;
        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int i) {
        return i == 0 || i == 21 || i == 30;
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitleList, tvTextList;
        ImageView mImageView;

        public RadioGroup mRadioGroup;
        public RadioButton mRadioButtonConform;
        public RadioButton mRadioButtonNotApplicable;
        public RadioButton mRadioButtonNotConform;

        RelativeLayout buttonLayoutArrow;
        public LinearLayout expandableLayout;

        public ViewHolder(View view) {
            super(view);

            tvTitleList = view.findViewById(R.id.textView_title);
            tvTextList = view.findViewById(R.id.textView_subTitle);
            mImageView = view.findViewById(R.id.photo);
            mRadioGroup = view.findViewById(R.id.radio_group);
            mRadioButtonConform = view.findViewById(R.id.radio_conform);
            mRadioButtonNotApplicable = view.findViewById(R.id.radio_not_applicable);
            mRadioButtonNotConform = view.findViewById(R.id.radio_not_conform);

            buttonLayoutArrow = view.findViewById(R.id.btnArrow);
            expandableLayout = view.findViewById(R.id.expandableLayout);
        }
    }

    //Header
    public class HeaderVh extends ExpandableRecyclerAdapter.ViewHolder {

        @BindView(R.id.header_id)
        public TextView headerTitle;

        public HeaderVh(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public class ItemVh extends ExpandableRecyclerAdapter.ViewHolder {

        @BindView(R.id.textView_title)
        public TextView itemContent;


        public ItemVh(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private void onClickButton(final LinearLayout expandableLayout, final RelativeLayout buttonLayout, final int i) {

        //Expand CardView
        if (expandableLayout.getVisibility() == View.VISIBLE) {
            createRotateAnimator(buttonLayout, 180f, 0f).start();
            expandableLayout.setVisibility(View.GONE);
            expandState.put(i, false);
        } else {
            createRotateAnimator(buttonLayout, 0f, 180f).start();
            expandableLayout.setVisibility(View.VISIBLE);
            expandState.put(i, true);
        }

    }

    //Animation Expand
    private ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
        animator.setDuration(300);
        animator.setInterpolator(new LinearInterpolator());
        return animator;

    }}

1 answer

0

First excuse anything, but I’m typing from the cell phone

From what I understand, your problem is at the moment of adding the Radio you identified that was pressed in your lists (listId, listIDRadio, etc).

Note that you are adding the elements referring to Radiobuttons in the lists within the onCheckedChanged Eventlistener, that is, the code within that method is called every time the Radio that is selected is changed!

To solve this you can set aside the Eventlistener and simply use the method mRadioGroup.getCheckedRadioButtonId(), that returns -1 if no Radio is selected or then returns the Id of the selected button (int)

Also note that if you have ids for each of your Radiobuttons in your layout, you can take this id returned by the function and check if Radio was pressed by its name in the layout:


if(myRadioId == R.id.nome_do_radio){

//Executa algo
}

Again I ask you to excuse me if there is something wrong! I am at 4:40 of a night of insomnia typing in cell phone hehe

  • Hello Quik19, first of all thank you for the return and taking the time to help me, good about this case until I understood what happened to me, but not being able to implement... it may be for lack of experience, not being able to use the getCheckedRadioButtonId() in my recyclerView and get the results in my Activity that manages recyclerView and the Save button. You would have some code that works with that logic?

  • I don’t know how your code is structured to know what your classes can see or not, but the idea is that you add some Save button, and inside the onClick() of this button you recover the Radiogroup data (which Radiobutton was selected). This way you will not use a listener, or at each user selection the information will be updated. Also note that getCheckedRadioButtonId() is a Radiogroup method, not Reciclerview. Vc tbm can (and should) retrieve this information directly in your Activity that has Radiogroup instead of in the Adapter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.