【发布时间】:2019-07-26 15:45:34
【问题描述】:
我无法从我的适配器到活动中获取有关选中单选按钮的信息
这是单选按钮所在的回收站视图中的项目:
private boolean mRadioButton;
private String mCourseName, mHolesTxt, mHolesNm, mParTxt, mParNm;
public NewGameCourseItem(boolean radioButton, String courseName, String holesTxt, String holesNm, String parTxt, String parNm) {
mRadioButton = radioButton;
mCourseName = courseName;
mHolesTxt = holesTxt;
mHolesNm = holesNm;
mParTxt = parTxt;
mParNm = parNm;
}
public boolean getRadioButton() {
return mRadioButton;
}
public String getCourseName() {
return mCourseName;
}
public String getHolesTxt() {
return mHolesTxt;
}
public String getHolesNm() {
return mHolesNm;
}
public String getParTxt() {
return mParTxt;
}
public String getParNm() {
return mParNm;
}
这是我的适配器,它会跟踪选择了哪个单选按钮:
public class ActivityNewGame2 extends AppCompatActivity {
private RecyclerView mRecyclerView;
private NewGameCourseAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<NewGameCourseItem> mCourseList;
@Override
public void onBindViewHolder(@NonNull final NewGameCourseViewHolder holder, final int position) {
/** This can prevent some unwanted actions in some cases **/
holder.mRadioButton.setOnCheckedChangeListener(null);
holder.mRadioButton.setChecked(selectedPosition == position);
holder.mRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
selectedPosition = holder.getAdapterPosition();
/** This part considers checking for radio buttons (checked or not) **/
if (selectedPosition == position) {
holder.mRadioButton.setChecked(true);
notifyDataSetChanged();
} else {
holder.mRadioButton.setChecked(false);
notifyDataSetChanged();
}
这是我的活动,我正在尝试检查该列表,这些单选按钮的位置,以及如果这些单选按钮中的任何一个为真,那么当单击“开始游戏”按钮时,意图进行另一个活动:
mStartGame = findViewById(R.id.button_start_game);
mStartGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < mCourseList.size(); i++) {
/** If any radio button is selected, then intent to another Activity **/
if (mCourseList.get(i).getRadioButton() == true) {
Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
}
}
});
目前,它不会打算进行其他活动...当我启动我的应用并单击不同的单选按钮时,它们选择正确...
【问题讨论】:
-
什么是“mCourseList”?可以调试一下,看看数据是否正确吗?
-
假设 mCourseList 包含单选按钮,该按钮在检查时有动作。 So when radio button selected assign it to variable and check that variable then apply your behavior while starting activity OR as i understand you want to only know either radio button selected or not then just keep as boolean if any of the radio button selected.跨度>