android - Recyclerview 从单选按钮获取数据
问题描述
我只想选择一个单选按钮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();
但我无法获得任何活动数据。它总是显示为空。另外,如果我在单选按钮上单击两次,则取消选择。
请问有什么不清楚的。任何帮助,将不胜感激。
谢谢 :)
解决方案
替换int clickedPos = ((Integer)cb.getTag()).intValue();
为int clickedPos =position;
推荐阅读
- reactjs - 如何在redux工具包中推送reducer状态的数组值?
- docker - docker swarm macvlan 网络问题
- python-requests - 如何将请求代码转换为scrapy?
- javascript - 检测用户是否存在于 Discord 客户端 (Discord.js)
- spring-batch - 从 Spring Batch 中的多个表中删除记录
- wso2 - 从特定 API 公开有限方法:WSO2 API Manager 版本 2.6.0 免费
- python - 以动态方式比较两个数据库中的表列表的最佳方法
- sql - 在三元关系中实施参照完整性
- javascript - 如何使用矩在数组中获得最近的下一次?
- java - toString方法输出的解释以及toString out和hashCode输出的区别