我有一个带有复选框的列表视图。我使用 SharedPreferences 来保存每个复选框的状态,但是当我重新打开应用程序时,不会选中相同的复选框。我认为让程序知道选择了哪个复选框有问题,但我不知道如何解决它。
MainActivity.java
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);
}
EmployeeModel.java
public class EmployeeModel {
private String name;
private int age;
private String lastName;
private boolean selected;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
我的适配器
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;
}
在此先感谢您的帮助!