Android listview复选框状态未正确保存
我有一个带有复选框的列表视图。我使用SharedReferences保存每个复选框的状态,但当我重新打开应用程序时,不会选中相同的复选框。我认为让程序知道选中了哪个复选框是错误的,但我不知道如何修复它 MainActivity.javaAndroid listview复选框状态未正确保存,android,listview,checkbox,sharedpreferences,android-arrayadapter,Android,Listview,Checkbox,Sharedpreferences,Android Arrayadapter,我有一个带有复选框的列表视图。我使用SharedReferences保存每个复选框的状态,但当我重新打开应用程序时,不会选中相同的复选框。我认为让程序知道选中了哪个复选框是错误的,但我不知道如何修复它 MainActivity.java public class MainActivity extends Activity { private ListView list; private List<EmployeeModel> employee; private EmployeeAd
public class MainActivity extends Activity {
private ListView list;
private List<EmployeeModel> employee;
private EmployeeAdapter adapter;
CheckBox button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
employee = new ArrayList<EmployeeModel>();
fillData("Nishant", "Shah", 25, false);
fillData("Chirag", "Shah", 22, false);
fillData("Utsav", "Kapuriya", 22, false);
list = (ListView) findViewById(R.id.listView1);
adapter = new EmployeeAdapter(getApplicationContext(), R.layout.simplerow, employee);
list.setAdapter(adapter);
}
private void fillData ( String name, String lastName, int age, boolean isSelected ) {
final EmployeeModel model = new EmployeeModel();
model.setName(name);
model.setLastName(lastName);
model.setAge(age);
model.setSelected(isSelected);
employee.add(model);
}
我的适配器
public class EmployeeAdapter extends BaseAdapter {
public List<EmployeeModel> employeeData;
private Context mContext;
private LayoutInflater mInflater;
Editor editor;
public EmployeeAdapter(Context context, int textViewResourceId, List<EmployeeModel> objects) {
this.employeeData = objects;
this.mContext = context;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences sharedPrefs = mContext.getSharedPreferences("sharedPrefs", 0);
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.simplerow, null);
holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
holder.chkTick = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
editor = sharedPrefs.edit();
final int pos = position;
holder.txtName.setText(employeeData.get(position).getName() + " " + employeeData.get(position).getLastName());
holder.chkTick.setChecked(sharedPrefs.getBoolean("CheckValue"+position, false));
holder.chkTick.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
employeeData.get(pos).setSelected(isChecked);
editor.putBoolean("CheckValue"+pos, isChecked);
editor.commit();
}
});
return convertView;
}
static class ViewHolder {
TextView txtName;
CheckBox chkTick;
}
公共类EmployeeAdapter扩展了BaseAdapter{
公开名单雇员数据;
私有上下文;
私人停车场;
编辑;
公共EmployeeAdapter(上下文上下文、int-textViewResourceId、列表对象){
this.employeeData=对象;
this.mContext=上下文;
mInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
SharedReferences SharedReferences=mContext.getSharedReferences(“SharedReferences”,0);
视窗座;
if(convertView==null){
holder=新的ViewHolder();
convertView=mInflater.inflate(R.layout.simplerow,null);
holder.txtName=(TextView)convertView.findViewById(R.id.textView1);
holder.chkTick=(复选框)convertView.findViewById(R.id.checkBox1);
convertView.setTag(支架);
}
否则{
holder=(ViewHolder)convertView.getTag();
}
editor=sharedPrefs.edit();
最终int pos=位置;
holder.txtName.setText(employeeData.get(position.getName()+“”+employeeData.get(position.getLastName());
holder.chkTick.setChecked(sharedPrefs.getBoolean(“CheckValue”+位置,false));
holder.chkTick.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
选择员工数据获取(位置设置)(已检查);
编辑器.putBoolean(“检查值”+pos,已检查);
commit();
}
});
返回视图;
}
静态类视窗夹{
TextView-txtName;
复选框chkTick;
}
提前感谢您的帮助!几天前,我尝试做类似的事情,但getView函数的参数位置似乎不正确 我必须重写适配器中的getCount函数以获得正确的值。除此之外,我认为您的代码中没有任何错误
@Override
public int getCount() {
return employeeData.size();
}
希望有帮助:)检查此选项可能会有帮助。它不起作用……我认为获取正确复选框的位置有问题。您可以尝试在oncheckedchangelistener中添加var位置。可能持有人被回收,当单击复选框时,该位置不是您要查找的位置。制作一份副本listener类中的变量位置,并将该变量用于SharedRef。感谢您的关注,但也失败了!我尝试在代码中添加一些System.out.print,我发现布尔值已正确保存到正确的位置。因此我猜问题与尝试在应用程序启动时检索复选框状态有关又来了。
@Override
public int getCount() {
return employeeData.size();
}