listview复选框状态未正确保存

时间:2013-06-15 14:25:41

标签: android listview checkbox sharedpreferences android-arrayadapter

我有一个带复选框的列表视图。我使用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;
     }

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

前几天我尝试做类似的事情,似乎getView函数的参数位置不正确。

我必须覆盖适配器中的getCount函数才能获得正确的值。除此之外,我认为你的代码没有错。

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

希望有所帮助:)