简体   繁体   English

在 Android Studio 中执行某个操作后,如何设置我的复选框默认选中?

[英]How do I set my checkbox checked by default after a certain action is performed in Android Studio?

everyone.每个人。 Please help with finding a solution to this problem.请帮助找到解决此问题的方法。 I would very much appreciate it, I have an activity with a list of checkboxes, A checkbox and after clicking it, the next action is called.我将非常感激,我有一个带有复选框列表的活动,一个复选框,单击它后,将调用下一个操作。 performed, and completed, Afterward.执行,并完成,之后。 the action is completed, the app returns to the action with a list of checkboxes and now I want the used checkbox to be checked by default and unable to be clicked again.动作完成,应用程序返回到带有复选框列表的动作,现在我希望默认选中使用的复选框并且无法再次单击。 Please help!请帮忙!

This is what I tried to do but was not successful checkbox becomes unchecked when I return to the action:这是我尝试做但未成功的操作,当我返回操作时复选框未选中:

P2106.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                Intent move = new Intent(GeneralRoute.this, ScanTest.class);
                startActivity(move);
                P2106.setChecked(true);
            }
        }
    });

your model class你的 model class

public class YourModel {

private boolean someBoolean;
private String someString;
private int someInt;

public boolean isSomeBoolean() {
    return someBoolean;
}

public void setSomeBoolean(boolean someBoolean) {
    this.someBoolean = someBoolean;
}

public String getSomeString() {
    return someString;
}

public void setSomeString(String someString) {
    this.someString = someString;
}

public int getSomeInt() {
    return someInt;
}

public void setSomeInt(int someInt) {
    this.someInt = someInt;
}
}

your activity你的活动

public class MainActivity extends AppCompatActivity {

private static ArrayList<YourModel> modelList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Add Your Model Or Data To ArrayList
    modelList.add(new YourModel());
    modelList.add(new YourModel());

    ListView listView = findViewById(R.id.youtListView);
    YourAdapter yourAdapter = new YourAdapter(this, R.id.youtLayoutListItem, modelList);
    listView.setAdapter(yourAdapter);


}
}

and your adapter和你的适配器

public class YourAdapter extends ArrayAdapter<YourModel> {

private ArrayList<YourModel> modelList;

public YourAdapter(@NonNull Context context, int resource, @NonNull List<YourModel> objects) {
    super(context, resource, objects);
    this.modelList = (ArrayList<YourModel>) objects;
}

@Override
public int getCount() {
    return modelList.size();
}

@Nullable
@Override
public YourModel getItem(int position) {
    return modelList.get(position);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if (convertView == null) {
            ...
    }
    YourModel model = modelList.get(position);
    P2106.setChecked(model.isSomeBoolean());
    P2106.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                Intent move = new Intent(GeneralRoute.this, ScanTest.class);
                startActivity(move);
                P2106.setChecked(true);
                model.setSomeBoolean(true);
                modelList.set(modelList.indexOf(model), model);
                notifyDataSetChanged();
            }
        }
    });
    return convertView;
}
}

Don't Forget to replace with your data不要忘记用您的数据替换

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ListView中设置复选框的默认状态[Android] - How do I set the default state of a checkbox in ListView [Android] 如何设置在arrayList中以特定条件检查的复选框? - How to set checkbox in arrayList checked with certain condition? Android Studios:如何编写代码,让我的应用程序在多次点击后执行特定操作? - Android Studios: How do I make a code that allows my app to do a certain action after a written number of clicks? Struts 1如何设置复选框默认选中 - Struts 1 How to set checkbox default checked Android-我怎么知道选中了哪个复选框? - Android - how do i know which checkbox is checked? 执行特定操作时如何删除对象? - How to delete an object when a certain action is performed? 当达到特定分数时,如何在我的骰子游戏中添加警报对话框? Java,Android工作室 - How do I add an Alert Dialog to my Dice Game when a certain score is reached? Java, Android Studio 在 android studio 中,如何仅在特定情况下为我的计算器应用程序允许小数点? - How do I allow a decimal point only in certain circumstance for my calculator app in android studio? 选中复选框后,如何使我的CheckBox执行某些操作? - How do I get my CheckBox to do something when it's checked? 如何在Android Studio中将默认的compileSdkVersion / targetSdkVersion设置为某些API级别 - How do I set default compileSdkVersion / targetSdkVersion to some API level in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM