Example #1
0
  /** Returns the view for a specific item on the list */
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    final ToDoItem currentItem = getItem(position);

    if (row == null) {
      LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
      row = inflater.inflate(mLayoutResourceId, parent, false);
    }

    row.setTag(currentItem);
    final CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkToDoItem);
    checkBox.setText(currentItem.getText());
    checkBox.setChecked(false);
    checkBox.setEnabled(true);

    checkBox.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View arg0) {
            if (checkBox.isChecked()) {
              checkBox.setEnabled(false);
              if (mContext instanceof ToDoActivity) {
                ToDoActivity activity = (ToDoActivity) mContext;
                activity.checkItem(currentItem);
              }
            }
          }
        });

    return row;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    final ToDoItem toDoItem = mItems.get(position);

    LayoutInflater inflater = LayoutInflater.from(mContext);
    RelativeLayout itemLayout = (RelativeLayout) inflater.inflate(R.layout.todo_item, null);

    final TextView titleView = (TextView) itemLayout.findViewById(R.id.titleView);
    titleView.setText(toDoItem.getTitle());

    final CheckBox statusView = (CheckBox) itemLayout.findViewById(R.id.statusCheckBox);

    statusView.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            log("Entered onCheckedChanged()");
            ToDoItem.Status status = isChecked ? ToDoItem.Status.DONE : ToDoItem.Status.NOTDONE;
            toDoItem.setStatus(status);
          }
        });

    final TextView priorityView = (TextView) itemLayout.findViewById(R.id.priorityView);
    priorityView.setText(toDoItem.getPriority().toString());

    final TextView dateView = (TextView) itemLayout.findViewById(R.id.dateView);
    dateView.setText(ToDoItem.FORMAT.format(toDoItem.getDate()));

    // Return the View you just created
    return itemLayout;
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    Log.i(TAG, "Entered onActivityResult()");

    // DONE - Check result code and request code
    // if user submitted a new ToDoItem
    // Create a new ToDoItem from the data Intent
    // and then add it to the adapter
    if (requestCode == ADD_TODO_ITEM_REQUEST) {

      Log.i(TAG, "Request code is ADD_TODO_ITEM_REQUEST");

      if (resultCode == RESULT_OK) {

        Log.i(TAG, "Result is OK, creating the new item...");
        // DONE - PM - implement Item creation

        ToDoItem newToDo = new ToDoItem(data);
        // DBug Toast
        Toast.makeText(
                getApplicationContext(),
                ("Title: "
                    + newToDo.getTitle()
                    + "\nSatus: "
                    + newToDo.getStatus().toString()
                    + "\nPriority: "
                    + newToDo.getPriority().toString()),
                Toast.LENGTH_LONG)
            .show();

        mAdapter.add(newToDo);
      }
    }
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

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

    // TODO - Inflate the View for this ToDoItem
    // from todo_item.xml.
    LayoutInflater inflater =
        (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout itemLayout = (RelativeLayout) inflater.inflate(R.layout.todo_item, null);

    // TODO - 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

    // TODO - Display Title in TextView

    final TextView titleView = (TextView) itemLayout.findViewById(R.id.titleView);
    titleView.setText(toDoItem.getTitle());

    // TODO - Set up Status CheckBox

    final CheckBox statusView = (CheckBox) itemLayout.findViewById(R.id.statusCheckBox);
    statusView.setChecked(toDoItem.getStatus().equals(Status.DONE));

    statusView.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            log("Entered onCheckedChanged()");

            // TODO - Set up and implement an OnCheckedChangeListener, which
            // is called when the user toggles the status checkbox
            if (isChecked) {
              toDoItem.setStatus(Status.DONE);
              statusView.setChecked(true);
            } else {
              toDoItem.setStatus(Status.NOTDONE);
              statusView.setChecked(false);
            }
          }
        });

    // TODO - Display Priority in a TextView

    final TextView priorityView = (TextView) itemLayout.findViewById(R.id.priorityView);
    priorityView.setText(toDoItem.getPriority().toString());

    // TODO - Display Time and Date.
    // Hint - use ToDoItem.FORMAT.format(toDoItem.getDate()) to get date and time String

    final TextView dateView = (TextView) itemLayout.findViewById(R.id.dateView);
    dateView.setText(ToDoItem.FORMAT.format(toDoItem.getDate()));

    // Return the View you just created
    return itemLayout;
  }
  /**
   * Mark an item as completed
   *
   * @param item The item to mark
   */
  public void checkItem(final ToDoItem item) {
    if (mClient == null) {
      return;
    }

    // Set the item as completed and update it in the table
    item.setComplete(true);

    AsyncTask<Void, Void, Void> task =
        new AsyncTask<Void, Void, Void>() {
          @Override
          protected Void doInBackground(Void... params) {
            try {

              checkItemInTable(item);
              runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      if (item.isComplete()) {
                        mAdapter.remove(item);
                      }
                    }
                  });
            } catch (final Exception e) {
              createAndShowDialogFromTask(e, "Error");
            }

            return null;
          }
        };

    runAsyncTask(task);
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    final ToDoItem toDoItem = (ToDoItem) getItem(position);

    RelativeLayout itemLayout = null;
    itemLayout =
        (RelativeLayout)
            LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_item, parent, false);

    // 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

    final TextView titleView = (TextView) itemLayout.findViewById(R.id.titleView);
    titleView.setText(toDoItem.getTitle());

    final CheckBox statusView = (CheckBox) itemLayout.findViewById(R.id.statusCheckBox);
    if (toDoItem.getStatus() == ToDoItem.Status.DONE) statusView.setChecked(true);
    else statusView.setChecked(false);

    statusView.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
              toDoItem.setStatus(ToDoItem.Status.DONE);
            } else {
              toDoItem.setStatus(ToDoItem.Status.NOTDONE);
            }
          }
        });

    final TextView priorityView = (TextView) itemLayout.findViewById(R.id.priorityView);
    priorityView.setText(toDoItem.getPriority().toString());

    // Hint - use ToDoItem.FORMAT.format(toDoItem.getDate()) to get date and
    // time String
    final TextView dateView = (TextView) itemLayout.findViewById(R.id.dateView);
    dateView.setText(ToDoItem.FORMAT.format(toDoItem.getDate()));

    // Return the View you just created
    return itemLayout;
    // return convertView;
  }
  public List<ToDoItem> getAllItems() {
    List<ToDoItem> items = new ArrayList<>();
    String items_select_all;

    if (today) {
      items_select_all =
          ("SELECT * FROM "
              + TABLE_TODO
              + " WHERE "
              + " (strftime('%j', 'now','localtime') - strftime('%j', datetime("
              + KEY_DUE_DATE
              + "/1000, 'unixepoch', 'localtime'))) = 0 "
              + " ORDER BY "
              + KEY_DUE_DATE
              + " DESC");
    } else if (hideOldItems()) {
      items_select_all =
          ("SELECT * FROM "
              + TABLE_TODO
              + " WHERE "
              + " (strftime('%j', 'now', 'localtime') - strftime('%j', datetime("
              + KEY_DUE_DATE
              + "/1000, 'unixepoch', 'localtime'))) < 3 "
              + " ORDER BY "
              + KEY_DUE_DATE
              + " DESC");
    } else {
      items_select_all =
          String.format("SELECT * FROM %s ORDER BY " + KEY_DUE_DATE + " DESC", TABLE_TODO);
    }

    SQLiteDatabase db = getReadableDatabase();
    if (db == null) return null;
    Cursor c = db.rawQuery(items_select_all, null);

    try {
      if (c.moveToFirst()) {
        do {
          ToDoItem newItem = new ToDoItem();
          newItem.description = c.getString(c.getColumnIndex(KEY_DESCRIPTION));
          newItem.id = c.getInt(c.getColumnIndex(KEY_ID));
          newItem.priority = c.getInt(c.getColumnIndex(KEY_PRIORITY));
          newItem.modifiedDate = c.getLong(c.getColumnIndex(KEY_MODIFIED_DATE));
          newItem.dueDate = c.getLong(c.getColumnIndex(KEY_DUE_DATE));
          newItem.createdDate = c.getLong(c.getColumnIndex(KEY_CREATED_DATE));
          items.add(newItem);
        } while (c.moveToNext());
      }

    } catch (Exception e) {
      Log.e(TAG, "Exception caught while parsing cursor: " + e);
    } finally {
      if (c != null && !c.isClosed()) c.close();
    }

    return items;
  }
  /**
   * Add a new item
   *
   * @param view The view that originated the call
   */
  public void addItem(View view) {
    if (mClient == null) {
      return;
    }

    // Create a new item
    final ToDoItem item = new ToDoItem();

    item.setText(mTextNewToDo.getText().toString());
    item.setComplete(false);

    // Insert the new item
    AsyncTask<Void, Void, Void> task =
        new AsyncTask<Void, Void, Void>() {
          @Override
          protected Void doInBackground(Void... params) {
            try {
              final ToDoItem entity = addItemInTable(item);

              runOnUiThread(
                  new Runnable() {
                    @Override
                    public void run() {
                      if (!entity.isComplete()) {
                        mAdapter.add(entity);
                      }
                    }
                  });
            } catch (final Exception e) {
              createAndShowDialogFromTask(e, "Error");
            }
            return null;
          }
        };

    runAsyncTask(task);

    mTextNewToDo.setText("");
  }