Android Open Source - coursera-android-001 To Do List Adapter

From Project

Back to project page coursera-android-001.


The source code is released under:

MIT License

If you think the Android project coursera-android-001 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package course.labs.todomanager;
// w w w  . j a  v  a2  s  .c  o  m
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

import course.labs.todomanager.ToDoItem.Status;

public class ToDoListAdapter extends BaseAdapter {

  // List of ToDoItems
  private final List<ToDoItem> mItems = new ArrayList<ToDoItem>();
  private final Context mContext;

  private static final String TAG = "Lab-UserInterface";

  public ToDoListAdapter(Context context) {

    mContext = context;


  // Add a ToDoItem to the adapter
  // Notify observers that the data set has changed

  public void add(ToDoItem item) {


  // Clears the list adapter of all items.
  public void clear(){


  // Returns the number of ToDoItems

  public int getCount() {

    return mItems.size();


  // Retrieve the number of ToDoItems

  public Object getItem(int pos) {

    return mItems.get(pos);


  // Get the ID for the ToDoItem
  // In this case it's just the position

  public long getItemId(int pos) {

    return pos;


  //Create a View to display the ToDoItem 
  // at specified position in mItems

  public View getView(int position, View convertView, ViewGroup parent) {

    //Get the current ToDoItem
    final ToDoItem toDoItem = (ToDoItem) getItem(position);

    //Inflate the View for this ToDoItem from todo_item.xml
    final RelativeLayout itemLayout = (RelativeLayout) ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.todo_item, null);
    //Fill in specific ToDoItem data
    // Remember that the data that goes in this View
    // corresponds to the user interface elements defined 
    // in the layout file

    //Display Title in TextView
    final TextView titleView = (TextView) itemLayout.findViewById(;
    // Set up Status CheckBox
    final CheckBox statusView = (CheckBox) itemLayout.findViewById(;
        statusView.setChecked(toDoItem.getStatus() == Status.DONE);

        setBackgroundColor(toDoItem.getStatus() == Status.DONE, itemLayout);
    if (statusView != null) {
            statusView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    log("Entered onCheckedChanged()");

                    // Set up and implement an OnCheckedChangeListener, which
                    // is called when the user toggles the status checkbox
                    if (isChecked) {
                    } else {

                    setBackgroundColor(isChecked, itemLayout);

    //Display Priority in a TextView
    final TextView priorityView = (TextView) itemLayout.findViewById(;
    //Display Time and Date.
    // Hint - use ToDoItem.FORMAT.format(toDoItem.getDate()) to get date and time String
    final TextView dateView = (TextView) itemLayout.findViewById(;

    // Return the View you just created
    return itemLayout;


    private void setBackgroundColor(Boolean isDone, RelativeLayout itemLayout) {
        if (isDone) {
        } else {
  private void log(String msg) {
    try {
    } catch (InterruptedException e) {
    Log.i(TAG, msg);


Java Source Code List