Exemplo n.º 1
0
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_edit_note, container, false);

    titleEditText = (EditText) rootView.findViewById(R.id.titleEditText);
    contentEditText = (EditText) rootView.findViewById(R.id.contentEditText);

    if (getArguments() != null) {
      note =
          new Note(
              getArguments().getString(ARG_ID),
              getArguments().getString(ARG_TITLE),
              getArguments().getString(ARG_CONTENT));

      titleEditText.setText(note.getTitle());
      // Place cursor at the end of title
      titleEditText.setSelection(titleEditText.getText().length());
      contentEditText.setText(note.getContent());

      showKeyboard = getArguments().getBoolean(ARG_SHOW_KEYBOARD);
    }

    titleEditText.clearFocus();

    if (showKeyboard) {
      titleEditText.requestFocus();
    }

    getActivity().setTitle(getString(R.string.title_add_note));
    return rootView;
  }
Exemplo n.º 2
0
  public void saveNote() {
    String titleToSave = titleEditText.getText().toString().trim();
    String contentToSave = contentEditText.getText().toString().trim();

    if (!contentToSave.isEmpty() || !titleToSave.isEmpty()) {
      if (note == null) {
        ParseObject post = new ParseObject("Post");
        post.put("title", titleToSave);
        post.put("content", contentToSave);
        post.put("author", ParseUser.getCurrentUser());
        post.saveInBackground();
        callback.onNoteAdded(titleToSave, contentToSave);
      } else {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
        final String newTitleToSave = titleToSave;
        final String newContentToSave = contentToSave;
        query.getInBackground(
            note.getId(),
            new GetCallback<ParseObject>() {
              @Override
              public void done(ParseObject parseObject, ParseException e) {
                if (e == null) {
                  parseObject.put("title", newTitleToSave);
                  parseObject.put("content", newContentToSave);
                  callback.onNoteChanged(new Note(newTitleToSave, newContentToSave), note);
                  parseObject.saveInBackground();
                }
              }
            });
      }
    }
  }
Exemplo n.º 3
0
 public void updateNote(Note newNote, Note oldNote) {
   for (Note note : notes) {
     if (oldNote.getId().equals(note.getId())) {
       note.setTitle(newNote.getTitle());
       note.setContent(newNote.getContent());
       notes.remove(note);
       notes.add(0, note);
       notesRecyclerView.scrollToPosition(0);
       notesAdapter.notifyDataSetChanged();
       break;
     }
   }
 }
Exemplo n.º 4
0
 private void deleteNote(final Note note) {
   Log.d(LOG_TAG, "delete note: " + note);
   ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
   query.getInBackground(
       note.getId(),
       new GetCallback<ParseObject>() {
         @Override
         public void done(ParseObject parseObject, ParseException e) {
           if (e == null) {
             Log.d(LOG_TAG, "note deleted");
             parseObject.deleteInBackground();
             if (notesAdapter.getItemCount() == 0) {
               showEmptyView();
             }
           }
         }
       });
 }