【问题标题】:Recyclerview get data from radiobuttonRecyclerview 从单选按钮获取数据
【发布时间】:2018-05-15 14:18:52
【问题描述】:

我只想在recyclerView 中选择一个单选按钮,并在其Activity 中获取其数据。

我已经通过以下解决方案:

我做了一个解决方案:

private static int lastCheckedPos = -1;

    binding.radioButton.setChecked(mImagesList.get(position).isSelected());
    binding.radioButton.setTag(new Integer(position));

                //for default check in first item
                if(position == 0 && mImagesList.get(0).isSelected() && binding.radioButton.isChecked())
                {
                    radioButton = binding.radioButton;
                    lastCheckedPos = 0;
                }

                binding.radioButton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        RadioButton cb = (RadioButton)v;
                        int clickedPos = ((Integer)cb.getTag()).intValue();

                        if(cb.isChecked())
                        {
                            if(radioButton != null)
                            {
                                radioButton.setChecked(false);
                                mImagesList.get(lastCheckedPos).setSelected(false);
                            }

                            radioButton = cb;
                            lastCheckedPos = clickedPos;
                        }
                        else
                            radioButton = null;

                        mImagesList.get(clickedPos).setSelected(cb.isChecked());
                    }
                });

我用onBindViewHolder 方法写了这个。

现在要获取我在adapter 中写的数据:

public String getUserId() {

        if(lastCheckedPos == -1)
        {
            return null;
        } else {
            return mImagesList.get(lastCheckedPos).getUser_code();
        }
    }

并在活动中得到这个:

 userId = adapter.getUserId();

但我无法获得任何活动数据。它总是显示为空。此外,如果我在单选按钮上单击两次,则会取消选择。

如果有不清楚的地方请询问。任何帮助将不胜感激。

谢谢你:)

【问题讨论】:

    标签: android android-recyclerview android-adapter android-radiobutton


    【解决方案1】:

    int clickedPos = ((Integer)cb.getTag()).intValue(); 替换为int clickedPos =position;

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多