【问题标题】:Java: get Information from Adapter to Activity if any radio button is clicked or notJava:如果单击任何单选按钮,则从适配器获取信息到活动
【发布时间】: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.跨度>

标签: java android


【解决方案1】:

问题在这里您没有在选中复选框时更新对象类 试试这个:

在你的adaper:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 2014-10-17
    • 2023-03-04
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    相关资源
    最近更新 更多