reset password
Author Message
mjaved3
Posts: 4
Posted 21:17 Jul 16, 2017 |

One of the available options to mark a todo as done is using checkboxes. In this way, all the todo's from the user's input will have a corresponding checkbox and checking it would mean the task is done. Is this the way we're supposed to do it? Can we mark the task as 'done' once the user swipes it left/right? Because one issue with the checkbox approach is, the onCheckboxClicked method allows us to work with the view object only. Whereas a method like onSwiped lets us have the id of an item by using the viewHolder object. If we should use checkboxes only, what might be a way to get the item's id (not the id of the checkbox, the id of the item being generated)?

melaniekwon
Posts: 5
Posted 22:35 Jul 16, 2017 |

I'm don't think this is 'best practice' but the way I got the item id was from within bind method of ItemHolder class   (in ToDoListAdapter.java).

From there, I call setOnClickListener on the checkbox and override OnClick method.

 

public void bind(ItemHolder holder, int pos) {

    ...

  id = cursor.getLong(cursor.getColumnIndex(Contract.TABLE_TODO._ID));  //item id

    ...

  checkbox.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          // do stuff with item id here
      }
  });

    ...

}

Last edited by melaniekwon at 22:37 Jul 16, 2017.
BrianK
Posts: 25
Posted 22:48 Jul 16, 2017 |

1. Add a method to the ItemClickListener

2. Override/Implement the method in your MainActivity

3. Use that method under Bind

Should work.

mjaved3
Posts: 4
Posted 23:53 Jul 16, 2017 |
melaniekwon wrote:

I'm don't think this is 'best practice' but the way I got the item id was from within bind method of ItemHolder class   (in ToDoListAdapter.java).

From there, I call setOnClickListener on the checkbox and override OnClick method.

 

public void bind(ItemHolder holder, int pos) {

    ...

  id = cursor.getLong(cursor.getColumnIndex(Contract.TABLE_TODO._ID));  //item id

    ...

  checkbox.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          // do stuff with item id here
      }
  });

    ...

}

Thank you for your reply, this makes sense. Now, after doing this in the adapter, in order to update the database from the MainActivity (by using ContentValues object and put method), what method are you overriding for a checkbox? Are we supposed to use the onCheckBoxClicked method for this on the MainActivity? 

melaniekwon
Posts: 5
Posted 07:48 Jul 17, 2017 |

Personally didn't use onCheckboxClicked b/c that uses checkbox id, not todoitem id. To update the checkbox status in db, I created a static method in MainActivity.java. Then called that method inside onClick.

MainActivity.java

public static int updateTodoStatus(SQLiteDatabase db, long id, boolean isChecked) {...}

ToDoListAdapter.java

  checkbox.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          // do stuff with item id here
          MainActivity.updateTodoStatus(db, id, isChecked);
      }
  });

 

But if anybody has better way, would appreciate hearing it.

Last edited by melaniekwon at 07:51 Jul 17, 2017.