Recover Selected Radio Button Used Recyclerview

Asked

Viewed 744 times

1

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

Each of the radio butttons is related to an item in the list of objects that was passed to Adapter as parameter.

How do I get the selection of radio button selected? I have to do something with the radios Buttons in the method onBindViewHolder?

Below, follow 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;
            }
        }
    }
}
  • At what point do you want to take it? I believe you can use the LayoutManager to recover the ViewHolder and seek the value of RadioButton which is marked. The method onBindViewHolder is only to popular model values in Views of the list.

  • I would like to take the moment I select one of the radio button options.

  • My suggestion is to register the Adapter like the Listener of the event, or the event itself ViewHolder with a reference to the item model, and in this case update the model that is associated with the line instead of wanting to retrieve the value from the outside.

  • I’m sorry if I’ve been too Umb, but how do I do it?

  • I can come up with an answer, but it might take a little while to close.

1 answer

1

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

1) Register the Adapter as RadioGroup.OnCheckedChangeListener

In that case, we need to register the Adapter instead of ViewHolder and in addition, store the item’s position 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) Recover direct value from Adapter

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

Browser other questions tagged

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