poprawiono bazę room - metoda getNoteRoomDatabase synchronized

This commit is contained in:
Naiki00 2021-02-15 23:46:51 +01:00
parent 31f6572fb9
commit eb4997790f
8 changed files with 138 additions and 199 deletions

View File

@ -1,34 +0,0 @@
package com.example.notatkon;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(FirstFragment.this)
.navigate(R.id.action_FirstFragment_to_SecondFragment);
}
});
}
}

View File

@ -1,34 +0,0 @@
package com.example.notatkon;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
public class SecondFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(SecondFragment.this)
.navigate(R.id.action_SecondFragment_to_FirstFragment);
}
});
}
}

View File

@ -10,6 +10,9 @@ import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import com.example.notatkon.dao.NoteDao;
import com.example.notatkon.entities.NoteEntity;
@Database(entities = {NoteEntity.class},
version = 1,
exportSchema = false)
@ -18,18 +21,16 @@ public abstract class NoteRoomDatabase extends RoomDatabase {
public abstract NoteDao noteDao();
private static volatile NoteRoomDatabase INSTANCE;
private static NoteRoomDatabase noteRoomDatabase;
public static NoteRoomDatabase getNoteRoomDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (NoteRoomDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
NoteRoomDatabase.class, "note_database")
.build();
}
}
public static synchronized NoteRoomDatabase getNoteRoomDatabase(final Context context) {
if (noteRoomDatabase == null) {
noteRoomDatabase = Room.databaseBuilder(
context,
NoteRoomDatabase.class,
"note_database"
).build();
}
return INSTANCE;
return noteRoomDatabase;
}
}

View File

@ -1,15 +1,17 @@
package com.example.notatkon;
package com.example.notatkon.note;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.notatkon.database.NoteEntity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.example.notatkon.R;
import com.example.notatkon.entities.NoteEntity;
import com.example.notatkon.database.NoteRoomDatabase;
import com.example.notatkon.note.CreateNote;
import java.util.List;
@ -107,14 +109,18 @@ public class MainActivity extends AppCompatActivity {
@Override
protected List<NoteEntity> doInBackground(Void... voids) {
return null;
//return null;
return (List<NoteEntity>) NoteRoomDatabase
.getNoteRoomDatabase(getApplicationContext())
.noteDao().getNotes();
}
@Override
protected void onPostExecute(List<NoteEntity> noteEntities) {
super.onPostExecute(noteEntities);
Log.d("NOTES", noteEntities.toString());
}
}
new GetNotesTask().execute();
}
}