From b514cad483ff4499fc94e024ebe643284cdc8450 Mon Sep 17 00:00:00 2001 From: Naiki00 Date: Wed, 17 Feb 2021 21:37:04 +0100 Subject: [PATCH] =?UTF-8?q?dodano=20wy=C5=9Bwietlanie=20si=C4=99=20zawarto?= =?UTF-8?q?=C5=9Bci=20istniej=C4=85cej=20notatki=20w=20edytorze=20po=20kli?= =?UTF-8?q?kni=C4=99ciu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/notatkon/adapter/NoteAdapter.java | 8 ++-- .../com/example/notatkon/note/CreateNote.java | 44 ++++++++++++++++--- .../example/notatkon/note/MainActivity.java | 5 ++- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/example/notatkon/adapter/NoteAdapter.java b/app/src/main/java/com/example/notatkon/adapter/NoteAdapter.java index 5d2829e..f4a1276 100644 --- a/app/src/main/java/com/example/notatkon/adapter/NoteAdapter.java +++ b/app/src/main/java/com/example/notatkon/adapter/NoteAdapter.java @@ -39,11 +39,11 @@ public class NoteAdapter extends RecyclerView.Adapter{ ViewHolder(@NonNull View itemView) { super(itemView); - noteTitle = (TextView) itemView.findViewById(R.id.note_title); - noteSubtitle = (TextView) itemView.findViewById(R.id.note_subtitle); - textDateTime = (TextView) itemView.findViewById(R.id.textDateTime); + noteTitle = itemView.findViewById(R.id.note_title); + noteSubtitle = itemView.findViewById(R.id.note_subtitle); + textDateTime = itemView.findViewById(R.id.textDateTime); - viewNote = (LinearLayout) itemView.findViewById(R.id.viewNote); + viewNote = itemView.findViewById(R.id.viewNote); } // wpisz notatke diff --git a/app/src/main/java/com/example/notatkon/note/CreateNote.java b/app/src/main/java/com/example/notatkon/note/CreateNote.java index 7d259b8..4dc17cd 100644 --- a/app/src/main/java/com/example/notatkon/note/CreateNote.java +++ b/app/src/main/java/com/example/notatkon/note/CreateNote.java @@ -8,6 +8,7 @@ import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; @@ -29,6 +30,8 @@ public class CreateNote extends AppCompatActivity { private EditText noteTitle, noteSubtitle, inputNote; private TextView textDataTime; + private NoteEntity selectedNote; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -61,23 +64,52 @@ public class CreateNote extends AppCompatActivity { saveNote(); } }); + + + if (getIntent().getBooleanExtra("update", false)) { + selectedNote = (NoteEntity) getIntent().getSerializableExtra("noteEntity"); + fillSelectedNote(); + } + } + + //przypisanie obecnej zawartosci notatki nowemu widokowi po kliknieciu + private void fillSelectedNote() { + noteTitle.setText(selectedNote.getTitle()); + noteSubtitle.setText(selectedNote.getSubtitle()); + inputNote.setText(selectedNote.getContent()); + textDataTime.setText(selectedNote.getDateTime()); } private void saveNote() { + if (noteTitle.getText().toString().trim().isEmpty()) { + Toast.makeText(this, "Nie może być puste!", Toast.LENGTH_SHORT).show(); + return; + } else if (noteSubtitle.getText().toString().trim().isEmpty() + && inputNote.getText().toString().trim().isEmpty()) { + Toast.makeText(this, "Nie może być puste!", Toast.LENGTH_SHORT).show(); + return; + } + //pobierz wpisywany tekst z textview - final NoteEntity note = new NoteEntity(); - note.setTitle(noteTitle.getText().toString()); - note.setSubtitle(noteSubtitle.getText().toString()); - note.setContent(inputNote.getText().toString()); - note.setDateTime(textDataTime.getText().toString()); + final NoteEntity noteEntity = new NoteEntity(); + noteEntity.setTitle(noteTitle.getText().toString()); + noteEntity.setSubtitle(noteSubtitle.getText().toString()); + noteEntity.setContent(inputNote.getText().toString()); + noteEntity.setDateTime(textDataTime.getText().toString()); + + + if (selectedNote != null) { + noteEntity.setId(selectedNote.getId()); + } + @SuppressLint("StaticFieldLeak") class SaveNoteTask extends AsyncTask { @Override protected Void doInBackground(Void... voids) { - NoteRoomDatabase.getNoteRoomDatabase(getApplicationContext()).noteDao().insert(note); + NoteRoomDatabase.getNoteRoomDatabase(getApplicationContext()).noteDao().insert(noteEntity); return null; } diff --git a/app/src/main/java/com/example/notatkon/note/MainActivity.java b/app/src/main/java/com/example/notatkon/note/MainActivity.java index eb34a25..3cd3bc2 100644 --- a/app/src/main/java/com/example/notatkon/note/MainActivity.java +++ b/app/src/main/java/com/example/notatkon/note/MainActivity.java @@ -31,6 +31,7 @@ public class MainActivity extends AppCompatActivity implements NoteListener { public static final int REQUEST_CODE_NEW_NOTE = 1; public static final int REQUEST_EDIT_NOTE = 2; + //public static final int REQUEST_SHOW_NOTE = 3; private int notePosition = -1; @@ -72,11 +73,11 @@ public class MainActivity extends AppCompatActivity implements NoteListener { @Override - public void onNoteClicked(NoteEntity noteEntity, int position) { + public void onNoteClicked(NoteEntity note, int position) { notePosition = position; Intent intent = new Intent(getApplicationContext(), CreateNote.class); intent.putExtra("update", true); - intent.putExtra("noteEntity", noteEntity); + intent.putExtra("noteEntity", note); startActivityForResult(intent, REQUEST_EDIT_NOTE); }