Get the ID and text of the RadioButton and manipulate the Data

0

Good morning, someone could help me with this question, how would I get the text and position of the radioButton put in an Arraylist and in my MainActivity that manages this RecyclerView to access this data.

Follow my Adapter

public class ExpandableRecyclerAdapter extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
    private List<Repo> repos;
    private SparseBooleanArray expandState = new SparseBooleanArray();
    public Context context;

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

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

    @NonNull
    @Override
    public ExpandableRecyclerAdapter.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.teste, viewGroup, false);
            return new ItemVh(view);
        }
        throw new RuntimeException("No macth for" + i + ".");
    }

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

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

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

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

            viewHolder.buttonLayoutArrow.setRotation(expandState.get(i) ? 180f : 0f);
            viewHolder.buttonLayoutArrow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickButton(viewHolder.expandableLayout, viewHolder.buttonLayoutArrow, i);
                    Log.i("log", "Item: " + i + " Expand");

                    /*viewHolder.mRadioButtonConform.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(context, "Conformed", Toast.LENGTH_SHORT).show();
                        }
                    });*/

                }
            });
        }
    }

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

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

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

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

        public RadioGroup mRadioGroup;
        public RadioButton mRadioButtonConform;
        public RadioButton mRadioButtonNotApplicable;
        public RadioButton mRadioButtonNotConform;
        public ArrayList<String> listRadio = new ArrayList<>();

        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);

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

                    int selectedRadioButtonID = mRadioGroup.getCheckedRadioButtonId();
                    RadioButton radioButton = group.findViewById(selectedRadioButtonID);
                    String selectedRadioButtonText = radioButton.getText().toString();
                    listRadio.add(selectedRadioButtonText);

                    if (checkedId == R.id.radio_conform) {
                        mRadioButtonConform.setChecked(true);
                        group.setTag(checkedId);
                        Log.i("log", "Item: " + listRadio + " checked " + getAdapterPosition() + " position");

                    } else if (checkedId == R.id.radio_not_applicable) {
                        mRadioButtonNotApplicable.setChecked(true);
                        group.setTag(checkedId);
                        Log.i("log", "Item: " + listRadio + " checked " + getAdapterPosition() + " position");

                    } else if (checkedId == R.id.radio_not_conform) {
                        mRadioButtonNotConform.setChecked(true);
                        group.setTag(checkedId);
                        Log.i("log", "Item: " + listRadio + " checked " + getAdapterPosition() + " position");

                    } else {
                        group.clearCheck();
                    }
                }
            });
            // End Test RadioButton
        }
    }

    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;

    }}

    
asked by anonymous 05.11.2018 / 18:07

0 answers