gpt4 book ai didi

Java:如果单击任何单选按钮,则从适配器获取信息到 Activity

转载 作者:行者123 更新时间:2023-11-30 10:01:55 25 4
gpt4 key购买 nike

我无法从我的适配器到 Activity 中获取有关已选中单选按钮的信息

这是该回收站 View 中单选按钮所在的项目:

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();
}

这是我的 Activity ,我试图在其中检查该列表,这些单选按钮在哪里,以及如果这些单选按钮中的任何一个为真,那么当单击“开始游戏”按钮时, Intent 进行另一个 Activity :

    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);
}
}

}
});

目前,它不会进行其他 Activity ...当我启动我的应用程序并单击不同的单选按钮时,它们会正确选择...

最佳答案

问题是你在选中复选框时没有更新你的对象类试试这个:

在您的适配器中:

if (selectedPosition == position) {
holder.mRadioButton.setChecked(true);

yourList.get(position).setRadioButton(holder.mRadioButton.isChecked)
notifyDataSetChanged();
} else {
holder.mRadioButton.setChecked(false);
yourList.get(position).setRadioButton(holder.mRadioButton.isChecked)
notifyDataSetChanged();
}

在您的 NewGameCourseItem 类中添加以下方法

public void setRadioButton(boolen isChecked) {
this.mRadioButton = isChecked;
}

关于Java:如果单击任何单选按钮,则从适配器获取信息到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57213683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com