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,120 +1,126 @@
package com.example.notatkon;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.notatkon.database.NoteEntity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_NEW_NOTE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageAddNote = findViewById(R.id.AddNote);
imageAddNote.setOnClickListener((v) -> {
startActivityForResult(
new Intent(getApplicationContext(), CreateNote.class),
REQUEST_CODE_NEW_NOTE
);
});
}
//Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
/*
https://developer.android.com/guide/components/activities/activity-lifecycle
https://developer.android.com/training/basics/intents/result
*/
/* wersja z fab
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(intent, REQUEST_CODE_NEW_NOTE);
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
}
});
/* -- Pierwsza wersja apki --
//metody
//https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#next-steps
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.notes);
//ustaw LayoutManagera wertykalnie
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//ustaw LayoutManagera horyzontalnie
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
//wczytaj listę z klasy Note oraz dodaj obiekt jej klasy
ArrayList<Note> notes = new ArrayList<Note>();
for (int i = 0; i < 20; i++) {
notes.add(new Note());
}
//połącz Adapter z RecycleView
recyclerView.setAdapter(new NoteAdapter(notes, recyclerView));
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
*/
//pobranie notatek z bazy i wyswietlanie na ekranie
private void getNotes() {
class GetNotesTask extends AsyncTask<Void, Void, List<NoteEntity>> {
@Override
protected List<NoteEntity> doInBackground(Void... voids) {
return null;
}
@Override
protected void onPostExecute(List<NoteEntity> noteEntities) {
super.onPostExecute(noteEntities);
}
}
}
package com.example.notatkon.note;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
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;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_NEW_NOTE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageAddNote = findViewById(R.id.AddNote);
imageAddNote.setOnClickListener((v) -> {
startActivityForResult(
new Intent(getApplicationContext(), CreateNote.class),
REQUEST_CODE_NEW_NOTE
);
});
}
//Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
/*
https://developer.android.com/guide/components/activities/activity-lifecycle
https://developer.android.com/training/basics/intents/result
*/
/* wersja z fab
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(intent, REQUEST_CODE_NEW_NOTE);
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
}
});
/* -- Pierwsza wersja apki --
//metody
//https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#next-steps
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.notes);
//ustaw LayoutManagera wertykalnie
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//ustaw LayoutManagera horyzontalnie
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
//wczytaj listę z klasy Note oraz dodaj obiekt jej klasy
ArrayList<Note> notes = new ArrayList<Note>();
for (int i = 0; i < 20; i++) {
notes.add(new Note());
}
//połącz Adapter z RecycleView
recyclerView.setAdapter(new NoteAdapter(notes, recyclerView));
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
*/
//pobranie notatek z bazy i wyswietlanie na ekranie
private void getNotes() {
class GetNotesTask extends AsyncTask<Void, Void, List<NoteEntity>> {
@Override
protected List<NoteEntity> doInBackground(Void... voids) {
//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();
}
}