[英]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.