执行特定操作后,如何将我的复选框设置为默认选中?

How do I set my checkbox checked by default after a certain action is performed?

我有一个带有复选框列表的 activity。一个复选框,单击它后,将调用、执行和完成下一个操作。之后,操作完成,应用程序 returns 进入带有复选框列表的操作,现在我希望默认选中已使用的复选框并且无法再次单击。请帮忙!

这是我尝试执行但未成功的操作,当我 return 执行以下操作时,复选框未选中:

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

你的模特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;
}
}

你的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);


}
}

和您的适配器

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

别忘了用你的数据替换