0

I have created Recycler view with header. And added items with dynamic radio button.

And

Here is my xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:weightSum="1"
    android:padding="@dimen/header_padding1"
    android:id="@+id/ac_ll"
    android:orientation="horizontal">

    <RadioGroup
        android:id="@+id/ic_radio_grp"
        android:layout_width="0dp"
        android:layout_weight="0.75"
        android:visibility="gone"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/header_padding" />

    <!--<LinearLayout
         android:id="@+id/checkbox_container"
         android:layout_width="0dp"
         app:multi_select="false"
         android:orientation="vertical"
         android:layout_margin="@dimen/header_padding"
         android:layout_height="match_parent"
         android:layout_weight="0.75"/>-->


    <CheckBox
        android:id="@+id/ic_checkbox_grp"
        android:layout_width="0dp"
        app:multi_select="false"
        app:orientation="vertical"
        android:layout_margin="@dimen/header_padding"
        android:layout_height="match_parent"
        android:textSize="18sp"
        android:text="ds"
        android:textColor="@color/rippile_color"
        android:textColorHint="@color/rippile_color"
        android:layout_weight="0.75" />

    <TextView
        android:id="@+id/price_txt"
        android:layout_width="0dp"
        android:layout_weight="0.25"
        android:layout_height="match_parent"
        android:padding="@dimen/header_padding1"
        android:text="$ 6 USD"
        android:maxLines="1"
        android:gravity="center"
        android:textSize="18sp"
        android:textColor="@color/rippile_color"
        android:textColorHint="@color/rippile_color" />

</LinearLayout>

And my adapter is

    public class AddCartItemChoiceAdapter extends 
    RecyclerView.Adapter<AddCartItemChoiceAdapter.ACItemViewHolder> {
    private final Context context;
    public ArrayList<HashMap<String, String>> arrayList;
    AdapterListener adapterListener;
    PreferenceHelper ph;
    String selectmultichoice = "";

    int multichk = 0;
    ArrayList<Integer> checkArray = new ArrayList();
    private RadioGroup lastCheckedRadioGroup = null;
    int lastselected;
    boolean isClicked = false;
    int shPosition = 0;
    ArrayList<HashMap<String, String>> listOfSelectedCheckBoxId = new ArrayList<HashMap<String, 
     String>>();

    public AddCartItemChoiceAdapter(Context context, int shPosition, String selectmultichoice, ArrayList<HashMap<String, String>> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
        this.selectmultichoice = selectmultichoice;
        this.shPosition = shPosition;
        // this.adapterListener = adapterListener;
        adapterListener = (AdapterListener) context;
        ph = new PreferenceHelper(context);
    }

    @Override
    public AddCartItemChoiceAdapter.ACItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ac_item_custom_row_layout, parent, false);
        return new AddCartItemChoiceAdapter.ACItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AddCartItemChoiceAdapter.ACItemViewHolder holder, @SuppressLint("RecyclerView") int position) {


        HashMap<String, String> aa = arrayList.get(position);


        if (arrayList.get(position).get("item_choice_price").equals("0")) {
            holder.priceTxt.setText("");
        } else {
            holder.priceTxt.setText("+$ " + Double.valueOf(arrayList.get(position).get("item_choice_price")));
        }

        if (selectmultichoice.equals("1")) {
            holder.icRadioGrp.setVisibility(View.VISIBLE);
            holder.icCheckboxGrp.setVisibility(View.GONE);
            int id = (position + 1) * 10;

            RadioButton rb = new RadioButton(AddCartItemChoiceAdapter.this.context);
            rb.setId(id++);
            rb.setText(arrayList.get(position).get("item_choice_name"));
            holder.icRadioGrp.addView(rb);

        } else {
            holder.icRadioGrp.setVisibility(View.GONE);
            holder.icCheckboxGrp.setVisibility(View.VISIBLE);

            holder.icCheckboxGrp.setText(arrayList.get(position).get("item_choice_name"));


        }


        holder.icRadioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {


                /*for(int irg=0; irg<radioGroup.getChildCount(); irg++) {
                    RadioButton btn = (RadioButton) radioGroup.getChildAt(irg);
                    if(btn.getId() == i) {
                        String text = btn.getText().toString();
                        // do something with text
                        Log.e("text  ", "===>" + text);
                    }
                }
            */
                Log.e("i  ", "===>" + i);
                if (lastCheckedRadioGroup != null
                        && lastCheckedRadioGroup.getCheckedRadioButtonId()
                        != radioGroup.getCheckedRadioButtonId()
                        && lastCheckedRadioGroup.getCheckedRadioButtonId() != -1) {
                    lastCheckedRadioGroup.clearCheck();
                    Log.e("Radio button clicked", "===> " + radioGroup.getCheckedRadioButtonId());
                    Log.e("lastCheckedRadioGroup ", "===> " + lastCheckedRadioGroup.getCheckedRadioButtonId());


                }
                lastCheckedRadioGroup = radioGroup;


            }
        });
        holder.icCheckboxGrp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isChecked = holder.icCheckboxGrp.isChecked();
                Log.e("position check ", "===>" + position);
                if (!isChecked) {

                    Log.e("uncheck", "===> " + isChecked);
                    // arrayList.get(position).put("ischecked", "false");

                    // listOfSelectedCheckBoxId.add(aa);
                } else {
                    Log.e("check", "===> " + isChecked);
                    //  arrayList.get(position).put("ischecked", "true");
                    // listOfSelectedCheckBoxId.add(aa);
                }

                //Log.e("listOfCheckBoxId", "===> " + listOfSelectedCheckBoxId);
            }
        });


    }

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

    class ACItemViewHolder extends RecyclerView.ViewHolder {

        public TextView priceTxt;
        public RadioGroup icRadioGrp;
        
        public CheckBox icCheckboxGrp;

        public LinearLayout acLL;

        public ACItemViewHolder(View itemView) {
            super(itemView);
            icRadioGrp = itemView.findViewById(R.id.ic_radio_grp);
            icCheckboxGrp = itemView.findViewById(R.id.ic_checkbox_grp);
            priceTxt = itemView.findViewById(R.id.price_txt);


        }


    }


}

I have tried to do two things here.

  1. Select any one option from the list
  2. To get the selected radio button text.

I am able to achieve first but I can't get the selected radio button text. Anybody can help me to resolve this? Thanks in advance.

Sreehari K
  • 887
  • 6
  • 16
malavika
  • 1,331
  • 4
  • 21
  • 54

0 Answers0