1

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

在此先感谢您的帮助!

4

1 回答 1

0

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

我必须重写适配器中的 getCount 函数才能获得正确的值。除此之外,我认为您的代码没有任何问题。

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

希望能帮助到你 :)

于 2013-06-15T14:38:20.063 回答