poprawiono bazę room - metoda getNoteRoomDatabase synchronized
This commit is contained in:
parent
31f6572fb9
commit
eb4997790f
@ -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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -10,6 +10,9 @@ import androidx.room.Database;
|
|||||||
import androidx.room.Room;
|
import androidx.room.Room;
|
||||||
import androidx.room.RoomDatabase;
|
import androidx.room.RoomDatabase;
|
||||||
|
|
||||||
|
import com.example.notatkon.dao.NoteDao;
|
||||||
|
import com.example.notatkon.entities.NoteEntity;
|
||||||
|
|
||||||
@Database(entities = {NoteEntity.class},
|
@Database(entities = {NoteEntity.class},
|
||||||
version = 1,
|
version = 1,
|
||||||
exportSchema = false)
|
exportSchema = false)
|
||||||
@ -18,18 +21,16 @@ public abstract class NoteRoomDatabase extends RoomDatabase {
|
|||||||
|
|
||||||
public abstract NoteDao noteDao();
|
public abstract NoteDao noteDao();
|
||||||
|
|
||||||
private static volatile NoteRoomDatabase INSTANCE;
|
private static NoteRoomDatabase noteRoomDatabase;
|
||||||
|
|
||||||
public static NoteRoomDatabase getNoteRoomDatabase(final Context context) {
|
public static synchronized NoteRoomDatabase getNoteRoomDatabase(final Context context) {
|
||||||
if (INSTANCE == null) {
|
if (noteRoomDatabase == null) {
|
||||||
synchronized (NoteRoomDatabase.class) {
|
noteRoomDatabase = Room.databaseBuilder(
|
||||||
if (INSTANCE == null) {
|
context,
|
||||||
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
|
NoteRoomDatabase.class,
|
||||||
NoteRoomDatabase.class, "note_database")
|
"note_database"
|
||||||
.build();
|
).build();
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return INSTANCE;
|
return noteRoomDatabase;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,120 +1,126 @@
|
|||||||
package com.example.notatkon;
|
package com.example.notatkon.note;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.util.Log;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import com.example.notatkon.database.NoteEntity;
|
import com.example.notatkon.R;
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.example.notatkon.entities.NoteEntity;
|
||||||
|
import com.example.notatkon.database.NoteRoomDatabase;
|
||||||
import java.util.List;
|
import com.example.notatkon.note.CreateNote;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
import java.util.List;
|
||||||
|
|
||||||
private static final int REQUEST_CODE_NEW_NOTE = 1;
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@Override
|
private static final int REQUEST_CODE_NEW_NOTE = 1;
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
@Override
|
||||||
setContentView(R.layout.activity_main);
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
ImageView imageAddNote = findViewById(R.id.AddNote);
|
setContentView(R.layout.activity_main);
|
||||||
imageAddNote.setOnClickListener((v) -> {
|
|
||||||
startActivityForResult(
|
ImageView imageAddNote = findViewById(R.id.AddNote);
|
||||||
new Intent(getApplicationContext(), CreateNote.class),
|
imageAddNote.setOnClickListener((v) -> {
|
||||||
REQUEST_CODE_NEW_NOTE
|
startActivityForResult(
|
||||||
);
|
new Intent(getApplicationContext(), CreateNote.class),
|
||||||
});
|
REQUEST_CODE_NEW_NOTE
|
||||||
}
|
);
|
||||||
|
});
|
||||||
//Toolbar toolbar = findViewById(R.id.toolbar);
|
}
|
||||||
//setSupportActionBar(toolbar);
|
|
||||||
|
//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
|
/*
|
||||||
*/
|
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() {
|
/* wersja z fab
|
||||||
@Override
|
FloatingActionButton fab = findViewById(R.id.fab);
|
||||||
public void onClick(View view) {
|
fab.setOnClickListener(new View.OnClickListener() {
|
||||||
startActivityForResult(intent, REQUEST_CODE_NEW_NOTE);
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
startActivityForResult(intent, REQUEST_CODE_NEW_NOTE);
|
||||||
// .setAction("Action", null).show();
|
|
||||||
}
|
//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
|
/* -- Pierwsza wersja apki --
|
||||||
|
//metody
|
||||||
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.notes);
|
//https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#next-steps
|
||||||
//ustaw LayoutManagera wertykalnie
|
|
||||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.notes);
|
||||||
|
//ustaw LayoutManagera wertykalnie
|
||||||
//ustaw LayoutManagera horyzontalnie
|
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||||
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
|
|
||||||
|
//ustaw LayoutManagera horyzontalnie
|
||||||
//wczytaj listę z klasy Note oraz dodaj obiekt jej klasy
|
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
|
||||||
ArrayList<Note> notes = new ArrayList<Note>();
|
|
||||||
for (int i = 0; i < 20; i++) {
|
//wczytaj listę z klasy Note oraz dodaj obiekt jej klasy
|
||||||
notes.add(new Note());
|
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));
|
|
||||||
|
//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.
|
@Override
|
||||||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
return true;
|
// 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
|
@Override
|
||||||
// automatically handle clicks on the Home/Up button, so long
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
// as you specify a parent activity in AndroidManifest.xml.
|
// Handle action bar item clicks here. The action bar will
|
||||||
int id = item.getItemId();
|
// automatically handle clicks on the Home/Up button, so long
|
||||||
|
// as you specify a parent activity in AndroidManifest.xml.
|
||||||
//noinspection SimplifiableIfStatement
|
int id = item.getItemId();
|
||||||
if (id == R.id.action_settings) {
|
|
||||||
return true;
|
//noinspection SimplifiableIfStatement
|
||||||
}
|
if (id == R.id.action_settings) {
|
||||||
|
return true;
|
||||||
return super.onOptionsItemSelected(item);
|
}
|
||||||
}
|
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
*/
|
}
|
||||||
|
|
||||||
//pobranie notatek z bazy i wyswietlanie na ekranie
|
*/
|
||||||
private void getNotes() {
|
|
||||||
|
//pobranie notatek z bazy i wyswietlanie na ekranie
|
||||||
class GetNotesTask extends AsyncTask<Void, Void, List<NoteEntity>> {
|
private void getNotes() {
|
||||||
|
|
||||||
@Override
|
class GetNotesTask extends AsyncTask<Void, Void, List<NoteEntity>> {
|
||||||
protected List<NoteEntity> doInBackground(Void... voids) {
|
|
||||||
return null;
|
@Override
|
||||||
}
|
protected List<NoteEntity> doInBackground(Void... voids) {
|
||||||
|
//return null;
|
||||||
@Override
|
return (List<NoteEntity>) NoteRoomDatabase
|
||||||
protected void onPostExecute(List<NoteEntity> noteEntities) {
|
.getNoteRoomDatabase(getApplicationContext())
|
||||||
super.onPostExecute(noteEntities);
|
.noteDao().getNotes();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
@Override
|
||||||
|
protected void onPostExecute(List<NoteEntity> noteEntities) {
|
||||||
|
super.onPostExecute(noteEntities);
|
||||||
|
Log.d("NOTES", noteEntities.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new GetNotesTask().execute();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user