dodano metodę zapisywania notatki + asynctask

This commit is contained in:
Naiki00 2021-02-15 21:56:22 +01:00
parent c08221f98d
commit a63f88fa7b

View File

@ -1,5 +1,8 @@
package com.example.notatkon; package com.example.notatkon;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
@ -10,11 +13,17 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.example.notatkon.database.NoteEntity; import com.example.notatkon.database.NoteEntity;
import com.example.notatkon.database.NoteRoomDatabase;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
/*
https://www.studytonight.com/android/get-edittext-set-textview#
https://stackoverflow.com/questions/4531396/get-value-of-a-edit-text-field
*/
public class CreateNote extends AppCompatActivity { public class CreateNote extends AppCompatActivity {
private EditText noteTitle, noteSubtitle, inputNote; private EditText noteTitle, noteSubtitle, inputNote;
@ -46,13 +55,32 @@ public class CreateNote extends AppCompatActivity {
private void saveNote() { private void saveNote() {
//pobierz wpisywany tekst z textview
final NoteEntity note = new NoteEntity(); final NoteEntity note = new NoteEntity();
note.setTitle(noteTitle.getText().toString()); note.setTitle(noteTitle.getText().toString());
note.setSubtitle(noteSubtitle.getText().toString()); note.setSubtitle(noteSubtitle.getText().toString());
note.setContent(inputNote.getText().toString()); note.setContent(inputNote.getText().toString());
note.setDateTime(textDataTime.getText().toString()); note.setDateTime(textDataTime.getText().toString());
@SuppressLint("StaticFieldLeak")
class SaveNoteTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
NoteRoomDatabase.getNoteRoomDatabase(getApplicationContext()).noteDao().insert(note);
return null;
} }
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
new SaveNoteTask().execute();
}
} }