Recover selected radio button used RecyclerView

1

I'm trying to make a RecyclerView , where each recycleritem has 3 radio button options.

Each of the radio butttons is related to an item in the object list that was passed to the adapter as a parameter.

How do I get the selected radio button selected? Do I have to do something with the radios buttons in the onBindViewHolder method?

Below is my adapter:

public class LikeListAdapter extends RecyclerView.Adapter<LikeListAdapter.LikeItemViewHolder> {

    private List<Goals> goalsList;

    public LikeListAdapter(List<Goals> goalsList) {
        this.goalsList = goalsList;
    }

    @Override
    public LikeItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        // Inflate the custom layout
        View view = inflater.inflate(R.layout.recycler_item_like, parent, false);
        return new LikeItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(LikeItemViewHolder holder, int position) {
        Goals goals = goalsList.get(position);
        if (goals != null && getItemCount() > 0) {
            holder.goalsDescriptionTextView.setText(goals.getDescription());
        }
    }

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

    public class LikeItemViewHolder extends RecyclerView.ViewHolder implements RadioGroup.OnCheckedChangeListener {
        @Bind(R.id.description_goalsTextView)
        TextView goalsDescriptionTextView;
        @Bind(R.id.happy_radio)
        RadioButton happyRadioButton;
        @Bind(R.id.soso_radio)
        RadioButton sosoRadioButton;
        @Bind(R.id.angry_radio)
        RadioButton angryRadioButton;
        @Bind(R.id.radio_like)
        RadioGroup radioLike;

        public LikeItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            radioLike.setOnCheckedChangeListener(this);

        }

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.e("group","" + group);
            Log.e("checkedId","" + checkedId);
            switch (group.getId()) {
                case (R.id.happy_radio):
                    Log.e("aqui", "happy");
                    break;
                case (R.id.soso_radio):
                    Log.e("aqui", "soso");
                    break;
                case (R.id.angry_radio):
                    Log.e("aqui", "angry");
                    break;
            }
        }
    }
}
    
asked by anonymous 19.10.2015 / 20:49

1 answer

1

My suggestion is to update the model for each event and then fetch this information directly from the model instead of looking at Views . This can be done in a very simple way:

1) Register the Adapter as RadioGroup.OnCheckedChangeListener

In this case, we need to register Adapter instead of ViewHolder and save the position of the item in the line item by a tag .

public class LikeListAdapter extends ... implements RadioGroup.OnCheckedChangeListener {

    // Todo resto do código continua o mesmo

    @Override
    public void onBindViewHolder(LikeItemViewHolder holder, int position) {
        Goals goals = goalsList.get(position);

        if (goals != null && getItemCount() > 0) {
            holder.goalsDescriptionTextView.setText(goals.getDescription());

            // Registrar o Adapter como o OnCheckedChangeListener
            holder.radioLike.setOnCheckedChangeListener(this);

            // Guardar a posicao do item como uma tag
            holder.radioLike.setTag(R.layout.recycler_item_like, position);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int position = (Integer) group.getTag(R.layout.recycler_item_like);

        Goals goals = goalsList.get(position);
        String checkedItem = null;

        switch (checkedId) {
            case (R.id.happy_radio):
                checkedItem = "happy";
                break;
            case (R.id.soso_radio):
                checkedItem = "soso";
                break;
            case (R.id.angry_radio):
                checkedItem = "angry";
                break;
        }

        // Setar o valor no goals
    }
}

2) Retrieve the direct value of Adapter

To recover the value, just retrieve the item from the Adapter and fetch the item.

    
19.10.2015 / 22:22