|
| 1 | +package com.d4rk.androidtutorials.java.ui.screens.android.lessons.data.room; |
| 2 | + |
| 3 | +import android.view.LayoutInflater; |
| 4 | +import android.view.View; |
| 5 | +import android.view.ViewGroup; |
| 6 | +import android.os.Bundle; |
| 7 | + |
| 8 | +import androidx.annotation.NonNull; |
| 9 | +import androidx.recyclerview.widget.LinearLayoutManager; |
| 10 | +import androidx.recyclerview.widget.RecyclerView; |
| 11 | + |
| 12 | +import com.d4rk.androidtutorials.java.R; |
| 13 | +import com.d4rk.androidtutorials.java.databinding.ActivityRoomBinding; |
| 14 | +import com.d4rk.androidtutorials.java.ui.components.navigation.UpNavigationActivity; |
| 15 | +import com.d4rk.androidtutorials.java.utils.EdgeToEdgeDelegate; |
| 16 | +import com.google.android.material.textview.MaterialTextView; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.ExecutorService; |
| 21 | +import java.util.concurrent.Executors; |
| 22 | + |
| 23 | +/** |
| 24 | + * Demonstrates basic Room usage by inserting and reading notes. |
| 25 | + */ |
| 26 | +public class RoomActivity extends UpNavigationActivity { |
| 27 | + private ActivityRoomBinding binding; |
| 28 | + private NotesAdapter adapter; |
| 29 | + private AppDatabase db; |
| 30 | + private final ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 31 | + |
| 32 | + @Override |
| 33 | + protected void onCreate(Bundle savedInstanceState) { |
| 34 | + super.onCreate(savedInstanceState); |
| 35 | + binding = ActivityRoomBinding.inflate(getLayoutInflater()); |
| 36 | + setContentView(binding.getRoot()); |
| 37 | + |
| 38 | + EdgeToEdgeDelegate edgeToEdgeDelegate = new EdgeToEdgeDelegate(this); |
| 39 | + edgeToEdgeDelegate.applyEdgeToEdge(binding.constraintLayout); |
| 40 | + |
| 41 | + db = AppDatabase.getInstance(this); |
| 42 | + |
| 43 | + binding.recyclerView.setLayoutManager(new LinearLayoutManager(this)); |
| 44 | + adapter = new NotesAdapter(); |
| 45 | + binding.recyclerView.setAdapter(adapter); |
| 46 | + |
| 47 | + loadNotes(); |
| 48 | + |
| 49 | + binding.buttonAdd.setOnClickListener(v -> { |
| 50 | + String text = String.valueOf(binding.editTextNote.getText()).trim(); |
| 51 | + if (!text.isEmpty()) { |
| 52 | + executor.execute(() -> { |
| 53 | + db.noteDao().insert(new Note(text)); |
| 54 | + runOnUiThread(() -> { |
| 55 | + binding.editTextNote.setText(""); |
| 56 | + loadNotes(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private void loadNotes() { |
| 64 | + executor.execute(() -> { |
| 65 | + List<Note> notes = db.noteDao().getAll(); |
| 66 | + runOnUiThread(() -> adapter.setNotes(notes)); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + private static class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteViewHolder> { |
| 71 | + private List<Note> notes = new ArrayList<>(); |
| 72 | + |
| 73 | + void setNotes(List<Note> notes) { |
| 74 | + this.notes = notes; |
| 75 | + notifyDataSetChanged(); |
| 76 | + } |
| 77 | + |
| 78 | + @NonNull |
| 79 | + @Override |
| 80 | + public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 81 | + View view = LayoutInflater.from(parent.getContext()) |
| 82 | + .inflate(R.layout.item_note, parent, false); |
| 83 | + return new NoteViewHolder(view); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) { |
| 88 | + holder.textView.setText(notes.get(position).getText()); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public int getItemCount() { |
| 93 | + return notes.size(); |
| 94 | + } |
| 95 | + |
| 96 | + static class NoteViewHolder extends RecyclerView.ViewHolder { |
| 97 | + final MaterialTextView textView; |
| 98 | + |
| 99 | + NoteViewHolder(View itemView) { |
| 100 | + super(itemView); |
| 101 | + textView = itemView.findViewById(R.id.textViewNote); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments