dodano wyświetlanie się zawartości istniejącej notatki w edytorze po kliknięciu

This commit is contained in:
Naiki00 2021-02-17 21:37:04 +01:00
parent 826e025f9e
commit b514cad483
3 changed files with 45 additions and 12 deletions

View File

@ -39,11 +39,11 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
ViewHolder(@NonNull View itemView) { ViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
noteTitle = (TextView) itemView.findViewById(R.id.note_title); noteTitle = itemView.findViewById(R.id.note_title);
noteSubtitle = (TextView) itemView.findViewById(R.id.note_subtitle); noteSubtitle = itemView.findViewById(R.id.note_subtitle);
textDateTime = (TextView) itemView.findViewById(R.id.textDateTime); textDateTime = itemView.findViewById(R.id.textDateTime);
viewNote = (LinearLayout) itemView.findViewById(R.id.viewNote); viewNote = itemView.findViewById(R.id.viewNote);
} }
// wpisz notatke // wpisz notatke

View File

@ -8,6 +8,7 @@ import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -29,6 +30,8 @@ public class CreateNote extends AppCompatActivity {
private EditText noteTitle, noteSubtitle, inputNote; private EditText noteTitle, noteSubtitle, inputNote;
private TextView textDataTime; private TextView textDataTime;
private NoteEntity selectedNote;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -61,23 +64,52 @@ public class CreateNote extends AppCompatActivity {
saveNote(); 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() { 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 //pobierz wpisywany tekst z textview
final NoteEntity note = new NoteEntity(); final NoteEntity noteEntity = new NoteEntity();
note.setTitle(noteTitle.getText().toString()); noteEntity.setTitle(noteTitle.getText().toString());
note.setSubtitle(noteSubtitle.getText().toString()); noteEntity.setSubtitle(noteSubtitle.getText().toString());
note.setContent(inputNote.getText().toString()); noteEntity.setContent(inputNote.getText().toString());
note.setDateTime(textDataTime.getText().toString()); noteEntity.setDateTime(textDataTime.getText().toString());
if (selectedNote != null) {
noteEntity.setId(selectedNote.getId());
}
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
class SaveNoteTask extends AsyncTask<Void, Void, Void> { class SaveNoteTask extends AsyncTask<Void, Void, Void> {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
NoteRoomDatabase.getNoteRoomDatabase(getApplicationContext()).noteDao().insert(note); NoteRoomDatabase.getNoteRoomDatabase(getApplicationContext()).noteDao().insert(noteEntity);
return null; return null;
} }

View File

@ -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_CODE_NEW_NOTE = 1;
public static final int REQUEST_EDIT_NOTE = 2; public static final int REQUEST_EDIT_NOTE = 2;
//public static final int REQUEST_SHOW_NOTE = 3;
private int notePosition = -1; private int notePosition = -1;
@ -72,11 +73,11 @@ public class MainActivity extends AppCompatActivity implements NoteListener {
@Override @Override
public void onNoteClicked(NoteEntity noteEntity, int position) { public void onNoteClicked(NoteEntity note, int position) {
notePosition = position; notePosition = position;
Intent intent = new Intent(getApplicationContext(), CreateNote.class); Intent intent = new Intent(getApplicationContext(), CreateNote.class);
intent.putExtra("update", true); intent.putExtra("update", true);
intent.putExtra("noteEntity", noteEntity); intent.putExtra("noteEntity", note);
startActivityForResult(intent, REQUEST_EDIT_NOTE); startActivityForResult(intent, REQUEST_EDIT_NOTE);
} }