title: Android Checkbox position in Custom ListView
body: <p>After hours of searching I'm unable to quite figure this out.</p>
  
  <p>I'm trying to save the checkbox state using shared preferences. As far as I know it's saving the state. However, the position is all off and I'm not sure what I'm doing wrong here. The btnToggle state works fine but the checkBox state which I'm not storing in the database is not and I don't want to have to use the database to store the state of that checkBox. Any help would be greatly appreciated!</p>
  
  <pre><code>public class AlarmListAdapter extends BaseAdapter {
  
  private Context mContext;
  private List&lt;AlarmModel&gt; mAlarms;
  Alarms displayAlarm = new Alarms();
  ViewHolder viewHolder;
  private CheckBox checkBox ;
  
  public AlarmListAdapter(Context context, List&lt;AlarmModel&gt; alarms) {
      mContext = context;
      mAlarms = alarms;
  
  }
  
  public void setAlarms(List&lt;AlarmModel&gt; alarms) {
      mAlarms = alarms;
  }
  
  @Override
  public int getCount() {
      if (mAlarms != null) {
          return mAlarms.size();
      }
      return 0;
  }
  
  @Override
  public Object getItem(int position) {
      if (mAlarms != null) {
          return mAlarms.get(position);
      }
      return null;
  }
  
  @Override
  public long getItemId(int position) {
      if (mAlarms != null) {
          return mAlarms.get(position).id;
      }
      return 0;
  }
  
  private class ViewHolder
  {
  
      RelativeLayout share;
      RelativeLayout fav;
      RelativeLayout info;
      CheckBox checkBox;
  
      Switch btnToggle;
  }
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
  
  
      final AlarmModel model = (AlarmModel) getItem(position);
  
  
      if(convertView == null) {
          LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
          convertView = mLayoutInflater.inflate(R.layout.alarm_list_item, null);
  
          viewHolder = new ViewHolder();
          viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.visible_toggle);
  
          viewHolder.btnToggle = (Switch) convertView.findViewById(R.id.alarm_item_toggle);
          convertView.setTag(viewHolder);
  
  
      }
      else{
  
          viewHolder = (ViewHolder) convertView.getTag();
          viewHolder.checkBox.getTag(position);
          viewHolder.btnToggle.getTag(position);
  
  
      }
  
  
  
  
      viewHolder.checkBox.setTag(Long.valueOf(model.id)); // This line is important.
  
      viewHolder.btnToggle.setTag(Long.valueOf(model.id));
  
      TextView txtTime = (TextView) convertView.findViewById(R.id.alarm_item_time);
      txtTime.setText(String.format("%02d:%02d", model.timeHour, model.timeMinute));
  
      TextView txtName = (TextView) convertView.findViewById(R.id.alarm_item_name);
      txtName.setText(model.name);
  
      updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_sunday), model.getRepeatingDay(AlarmModel.SUNDAY));
      updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_monday), model.getRepeatingDay(AlarmModel.MONDAY));
      updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_tuesday), model.getRepeatingDay(AlarmModel.TUESDAY));
      updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_wednesday), model.getRepeatingDay(AlarmModel.WEDNESDAY));
      updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_thursday), model.getRepeatingDay(AlarmModel.THURSDAY));
      updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_friday), model.getRepeatingDay(AlarmModel.FRDIAY));
      updateTextColor((TextView) convertView.findViewById(R.id.alarm_item_saturday), model.getRepeatingDay(AlarmModel.SATURDAY));
  
  
  
  
      viewHolder.btnToggle.setChecked(model.isEnabled);
      viewHolder.btnToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              ((AlarmSettings) mContext).setAlarmEnabled(((Long) buttonView.getTag()).longValue(), isChecked);
          }
      });
  
  
  
      viewHolder.checkBox.setChecked(getFromSP("visbox"));
      viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isVisible){
  
              viewHolder.checkBox.getTag(position);
  
  
              try {
                  saveInSp("visbox", isVisible);
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
              if(isVisible) {
  
               // Do stuff here
  
  
  
              }
              else {
  
  
  
  
  
              }
  
          }
  
  
      });
  
  
  
  
      convertView.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View view) {
              ((AlarmSettings) mContext).startAlarmDetailsActivity(((Long) view.getTag()).longValue());
          }
      });
  
      convertView.setOnLongClickListener(new View.OnLongClickListener() {
  
          @Override
          public boolean onLongClick(View view) {
              ((AlarmSettings) mContext).deleteAlarm((Long) view.getTag());
              return true;
          }
      });
  
  
  
  
      return convertView;
  }
  
  
  
  
  
  private void updateTextColor(TextView view, boolean isOn) {
      if (isOn) {
          view.setTextColor(mContext.getResources().getColor(R.color.primary_dark));
      } else {
          view.setTextColor(Color.BLACK);
      }
  }
  
  
  
  private boolean getFromSP(String key){
      SharedPreferences preferences = mContext.getSharedPreferences("epimelis.com.lyre", android.content.Context.MODE_PRIVATE);
      boolean val = preferences.getBoolean(key, false);
      System.out.println("getting key: "+ key + " with value: " + val);
      return val;
  }
  private void saveInSp(String key,boolean value) throws Exception {
      //if(!value) throw new Exception("Die Die Die");ooo
      System.out.println("Saving key: " + key + " with value: " + value);
      SharedPreferences preferences = mContext.getSharedPreferences("epimelis.com.lyre", android.content.Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = preferences.edit();
      editor.putBoolean(key, value);
      editor.commit();
  }
  </code></pre>
  
  <p>}</p>