title: Have to press twice to delete the 1st item and check box not getting marked
body: <p>I have created an application where users can store the activities they want and an alarm message would be generated when phone reaches the user selected time.Here user can mark the activities by pressing the checkbox and that will delete that activity from the table.But when I mark the 1st check box I have to click twice to remove it.Also the checkboxes don't get marked even when I press them. Please help.</p>
  
  <pre><code>public class ViewTask extends Activity {
  protected TaskerDbHelper db;
  List&lt;Task&gt; list;
  MyAdapter adapt;
  final Calendar c = Calendar.getInstance();
  int year = c.get(Calendar.YEAR);;
  int month = c.get(Calendar.MONTH);
  int day = c.get(Calendar.DAY_OF_MONTH);
  int hour = c.get(Calendar.HOUR_OF_DAY);
  int minute = c.get(Calendar.MINUTE);
  static final int DATE_DIALOG_ID = 0;
  static final int TIME_DIALOG_ID = 1;
   private ScheduleClient scheduleClient;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_view_task);
       scheduleClient = new ScheduleClient(this);
          scheduleClient.doBindService();
  
      db = new TaskerDbHelper(this);
  
      list = db.getAllTasks();
      adapt = new MyAdapter(this, R.layout.list_inner_view, list);
      ListView listTask = (ListView) findViewById(R.id.listView1);
      listTask.setAdapter(adapt);
  }
  
  public void chooseDate(View view)
  {
      showDialog(DATE_DIALOG_ID);
  }
  
  public void chooseTime(View view)
  {
      showDialog(TIME_DIALOG_ID);
  }
  
  private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
  
      @Override
      public void onDateSet(DatePicker view, int yearSelected,
              int monthOfYear, int dayOfMonth) {
          year = yearSelected;
          month = monthOfYear;
          day = dayOfMonth;
      }
  
  };
  
  private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
  
      @Override
      public void onTimeSet(TimePicker view, int hourOfDay, int min) {
          hour = hourOfDay;
          minute = min;
      }
  
  };
  
  protected Dialog onCreateDialog(int id) {
      switch (id) {
      case DATE_DIALOG_ID:
          return new DatePickerDialog(this, mDateSetListener, year, month,
                  day);
      case TIME_DIALOG_ID:
          return new TimePickerDialog(this, mTimeSetListener, hour, minute,
                  false);
  
      }
      return null;
  }
  
  public void addTaskNow(View v) {
      EditText t = (EditText) findViewById(R.id.editText1);
      String s = t.getText().toString();
      String s2 = year + "-" + month+1 + "-" + day + " " + hour + ":"
              + minute;
      System.out.println("Date is "+s2);
      if (s.equalsIgnoreCase("")) {
          Toast.makeText(this, "enter the task description first!!",
                  Toast.LENGTH_LONG);
      } 
      else {
          Task task = new Task(s, 0,s2);
          db.addTask(task);
          Log.d("tasker", "data added");
          t.setText("");
          adapt.add(task);
          System.out.println("Task name is "+task.getTaskName().toString());
          c.set(year,month,day,hour,minute,0);
            scheduleClient.setAlarmForNotification(c,task.getTaskName().toString());
          adapt.notifyDataSetChanged();
      }
  
  }
  
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_view_task, menu);
      return true;
  }
  
  private class MyAdapter extends ArrayAdapter&lt;Task&gt; {
  
      Context context;
      List&lt;Task&gt; taskList = new ArrayList&lt;Task&gt;();
      int layoutResourceId;
  
      public MyAdapter(Context context, int layoutResourceId,
              List&lt;Task&gt; objects) {
          super(context, layoutResourceId, objects);
          this.layoutResourceId = layoutResourceId;
          this.taskList = objects;
          this.context = context;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
  
          CheckBox chk = null;
          if (convertView == null) {
              LayoutInflater inflater = (LayoutInflater) context
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater.inflate(R.layout.list_inner_view,
                      parent, false);
              chk = (CheckBox) convertView.findViewById(R.id.chkStatus);
              convertView.setTag(chk);
  
              chk.setOnClickListener(new View.OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      CheckBox cb = (CheckBox) v;
                      Task changeTask = (Task) cb.getTag();
                      changeTask.setStatus(cb.isChecked() == true ? 1 : 0);
                      db.updateTask(changeTask);
                      db.delete(String.valueOf(changeTask.getId()));
                      Toast.makeText(
                              getApplicationContext(),
                              "Task completed " + cb.getText() + " is "
                                      + cb.isChecked(), Toast.LENGTH_LONG)
                              .show();
                      adapt.clear();
                      adapt.addAll(db.getAllTasks());
                      adapt.notifyDataSetChanged();
                  }
  
              });
          } else {
              chk = (CheckBox) convertView.getTag();
          }
          Task current = taskList.get(position);
          chk.setText(current.getTaskName()+" - "+current.getDate());
          chk.setChecked(current.getStatus() == 1 ? true : false);
          chk.setTag(current);
          Log.d("listener", String.valueOf(current.getId()));
          return convertView;
      }
  
  }
  
  }
  </code></pre>
  
  <p>Below is my DB connection class:</p>
  
  <pre><code>public class TaskerDbHelper extends SQLiteOpenHelper {
  
  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_NAME = "taskManagerTestt";
  private static final String TABLE_TASKS = "tasks";
  private static final String KEY_ID = "id";
  private static final String KEY_TASKNAME = "taskName";
  private static final String KEY_STATUS = "status";
  private static final String KEY_DATE="date";
  
  public TaskerDbHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  public void onCreate(SQLiteDatabase db) {
  
      String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_TASKS + " ( "
              + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TASKNAME
              + " TEXT, " + KEY_STATUS + " INTEGER, "+KEY_DATE+" TEXT )";
      db.execSQL(sql);
  
  //  db.close();
  }
  
  public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);
      onCreate(db);
  }
  
  public void addTask(Task task) {
      SQLiteDatabase db = this.getWritableDatabase();
  
      ContentValues values = new ContentValues();
      values.put(KEY_TASKNAME, task.getTaskName()); 
      values.put(KEY_STATUS, task.getStatus());
      values.put(KEY_DATE, task.getDate().toString());
      db.insert(TABLE_TASKS, null, values);
      db.close(); // Closing database connection
  }
  
  public List&lt;Task&gt; getAllTasks() {
      List&lt;Task&gt; taskList = new ArrayList&lt;Task&gt;();
      String selectQuery = "SELECT  * FROM " + TABLE_TASKS;
  
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery(selectQuery, null);
      if (cursor.moveToFirst()) {
          do {
              Task task = new Task();
              task.setId(cursor.getInt(0));
              task.setTaskName(cursor.getString(1));
              task.setStatus(cursor.getInt(2));
              task.setDate(cursor.getString(3));
              taskList.add(task);
          } while (cursor.moveToNext());
      }
      return taskList;
  }
  
  public void updateTask(Task task) {
      // updating row
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put(KEY_TASKNAME, task.getTaskName());
      values.put(KEY_STATUS, task.getStatus());
      values.put(KEY_DATE, task.getDate());
      db.update(TABLE_TASKS, values, KEY_ID + " = ?",new String[] {String.valueOf(task.getId())});
      db.close();
  }
  
  public void delete(String id)
  {
      SQLiteDatabase db = this.getWritableDatabase();
      db.delete(TABLE_TASKS,  KEY_ID + " = ?",new String[] {id});
  }
  
  public ArrayList getTaskForTheDay(String date)
  {
      String query="SELECT * FROM "+TABLE_TASKS+" WHERE "+KEY_DATE+" = "+"'"+date+"'";
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery(query, null);
      ArrayList tasks=new ArrayList();
      int count=0;
      // looping through all rows and adding to list
      if (cursor.moveToFirst()) {
          do {
              System.out.println("Came inside");
              Task task = new Task();
              task.setId(cursor.getInt(0));
              task.setTaskName(cursor.getString(1));
              tasks.add(task.getTaskName().toString());
              System.out.println("Task to notify is "+tasks.get(count));
              count++;
          } while (cursor.moveToNext());
      }
  
      return tasks;
  }
  
  }
  </code></pre>
  
  <p>And finally the class where getters and setters are being used:</p>
  
  <pre><code>public class Task {
  private String taskName;
  private int status;
  private int id;
  private String date;
  
  public Task()
  {
      this.taskName=null;
      this.status=0;
  }
  public Task(String taskName, int status,String date) {
      super();
      this.taskName = taskName;
      this.status = status;
      this.date=date;
  }
  
  public int getId() {
      return id;
  }
  public void setId(int id) {
      this.id = id;
  }
  public String getTaskName() {
      return taskName;
  }
  public void setTaskName(String taskName) {
      this.taskName = taskName;
  }
  public int getStatus() {
      return status;
  }
  public void setStatus(int status) {
      this.status = status;
  }
  
  public String getDate()
  {
      return date;
  }
  
  public void setDate(String date)
  {
      this.date=date;
  }
  
  }
  </code></pre>