Professional Documents
Culture Documents
Lớp NotesDatabase
-Đoạn mã trên định nghĩa một lớp NotesDatabase, là một lớp trừu tượng
kế thừa từ RoomDatabase, được sử dụng để tạo và quản lý cơ sở dữ liệu
Room trong ứng dụng Android. Dưới đây là giải thích từng phần:
-Lớp Notes:
1. @Entity(tableName = "Notes_Database"): Annotation này đánh dấu
lớp Notes là một entity trong cơ sở dữ liệu Room. Trong đó:
tableName = "Notes_Database": Xác định tên của bảng trong cơ
sở dữ liệu. Trong trường hợp này, tên bảng là
"Notes_Database".
2. @PrimaryKey(autoGenerate = true): Annotation này xác định trường
id là khóa chính của entity, và nó sẽ tự động tăng dần khi một
bản ghi mới được chèn vào cơ sở dữ liệu.
3. @ColumnInfo(name = "notes_title"): Annotation này đánh dấu trường
notesTitle và xác định tên của cột tương ứng trong bảng cơ sở dữ
liệu là "notes_title".
4. public int id;: Trường id được sử dụng làm khóa chính của entity,
và nó được tự động tăng dần.
5. public String notesTitle;, public String notesSubTitle;, public String
notesDate;, public String notes;, public String notesPriority;: Các trường
này là các cột trong bảng cơ sở dữ liệu. Mỗi trường tương ứng
với một cột trong bảng, và thông qua các Annotation
@ColumnInfo, bạn có thể xác định tên của các cột này.
Tổng thể, lớp Notes đóng vai trò là một mô hình dữ liệu (data model)
trong ứng dụng, định nghĩa cấu trúc dữ liệu của một ghi chú (note)
trong cơ sở dữ liệu Room, bao gồm các trường như tiêu đề, phụ đề,
ngày, nội dung và mức độ ưu tiên.
-Lớp NotesRepository
Tổng thể, lớp NotesRepository đóng vai trò trung gian giữa ViewModel
và cơ sở dữ liệu, cung cấp dữ liệu từ cơ sở dữ liệu cho ViewModel
thông qua các LiveData, và cung cấp các phương thức để thực hiện
các thao tác CRUD (create, read, update, delete) trên dữ liệu.
-Lớp NotesViewModel
Lớp NotesViewModel trong đoạn mã trên là một lớp ViewModel trong
kiến trúc MVVM (Model-View-ViewModel) của ứng dụng Android.
Dưới đây là giải thích từng phần của lớp này:
Tóm lại, NotesViewModel là lớp trung gian giữa View (Activity hoặc
Fragment) và Repository, cung cấp dữ liệu từ repository cho View và
xử lý các tương tác từ View để thay đổi dữ liệu. Lớp này giúp tách biệt
logic kinh doanh và giao diện người dùng trong ứng dụng.
-Lớp AddNotesActivity
Trong AddNoteActivity, có một số hành động được thực hiện:
Tóm lại, trong AddNoteActivity, người dùng có thể nhập thông tin cho
một ghi chú mới và chọn mức độ ưu tiên tương ứng. Khi nhấn vào nút
"Done", ghi chú mới sẽ được tạo và lưu vào cơ sở dữ liệu.
-Lớp SplashActivity
Activity SplashActivity trong mã trên là một hoạt động khởi đầu trong ứng dụng Android, được
sử dụng để hiển thị màn hình chào mừng ngắn trước khi chuyển đến màn hình chính của ứng
dụng. Dưới đây là giải thích về mã này:
1. Khai báo và khởi tạo: Trong phương thức onCreate(), định dạng giao diện của
SplashActivity được thiết lập bằng cách sử dụng phương thức setContentView() để đặt
layout từ tệp activity_splash.xml. Sau đó, thanh action bar được ẩn đi bằng cách gọi
getSupportActionBar().hide() .
2. Sử dụng Handler và postDelayed(): Một Handler được sử dụng để tạm dừng việc
chuyển đến màn hình chính (MainActivity) trong một khoảng thời gian nhất định. Phương
thức postDelayed() được gọi với thời gian trễ là 3000 milliseconds (tức là 3 giây). Trong
phương thức run() của Runnable, một Intent được tạo để chuyển đến MainActivity và
finishAffinity() được gọi để kết thúc tất cả các hoạt động hiện tại trong ngăn xếp hoạt
động.
3. Chuyển đến MainActivity: Sau khi thời gian trễ đã kết thúc, Intent được gọi để chuyển
đến MainActivity. finishAffinity() đảm bảo rằng tất cả các hoạt động trước đó trong ngăn
xếp hoạt động cũng sẽ bị kết thúc, vì vậy khi người dùng nhấn nút Back, họ sẽ không
quay lại màn hình chào mừng.
Tóm lại, SplashActivity là một hoạt động khởi đầu ngắn trong ứng dụng Android, được sử dụng
để hiển thị màn hình chào mừng trước khi chuyển đến màn hình chính của ứng dụng.
-Lớp UploadNoteActivity
Activity UpdateNoteActivity trong đoạn mã trên là một hoạt động trong ứng dụng Android được
sử dụng để cập nhật ghi chú đã tồn tại. Dưới đây là giải thích về mã này:
1. Khai báo và khởi tạo: Trong phương thức onCreate(), định dạng giao diện của
UpdateNoteActivity được thiết lập bằng cách sử dụng phương thức setContentView() để đặt
layout từ tệp activity_update_note.xml.
2. Nhận dữ liệu từ Intent và hiển thị: Dữ liệu về tiêu đề, phụ đề, nội dung và mức độ ưu
tiên của ghi chú cần được cập nhật được nhận từ Intent và hiển thị trên giao diện người
dùng.
3. Thiết lập sự kiện click cho các nút ưu tiên (priority): Tương tự như trong các hoạt
động khác, các nút ưu tiên được gán các sự kiện click tương ứng để người dùng có thể
chọn mức độ ưu tiên của ghi chú.
4. Cập nhật ghi chú: Khi người dùng nhấn nút "Update Note", dữ liệu mới của ghi chú
(tiêu đề, phụ đề, nội dung và mức độ ưu tiên) được lấy ra từ các trường nhập liệu và gửi
đến ViewModel để thực hiện cập nhật trong cơ sở dữ liệu.
5. Hiển thị menu xóa và xử lý sự kiện xóa: Trong phương thức onCreateOptionsMenu(),
menu được thiết lập bằng cách sử dụng getMenuInflater().inflate() để tạo menu từ tệp
delete_menu.xml. Khi người dùng nhấn vào menu xóa, một BottomSheetDialog được hiển
thị để xác nhận xóa ghi chú. Nếu người dùng xác nhận, ghi chú sẽ được xóa khỏi cơ sở
dữ liệu thông qua ViewModel.
Tóm lại, UpdateNoteActivity cho phép người dùng cập nhật thông tin của ghi chú đã tồn tại và
cung cấp chức năng xóa ghi chú.
package com.trodev.myethicnotes.activities;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.trodev.myethicnotes.Model.Notes;
import com.trodev.myethicnotes.R;
import com.trodev.myethicnotes.ViewModel.NotesViewModel;
import com.trodev.myethicnotes.databinding.ActivityUpdateNoteBinding;
import java.util.Date;
ActivityUpdateNoteBinding binding;
public String priority = "1";
String stitle, ssubtitle, snotes, spriority;
int sid ;
NotesViewModel notesViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityUpdateNoteBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().setTitle("Update Ethic");
if(spriority.equals("1"))
{
binding.greenPriority.setImageResource(R.drawable.ic_done);
}
else if(spriority.equals("2"))
{
binding.yellowPriority.setImageResource(R.drawable.ic_done);
}
else if(spriority.equals("3"))
{
binding.redPriority.setImageResource(R.drawable.ic_done);
}
binding.greenPriority.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.greenPriority.setImageResource(R.drawable.ic_done);
binding.yellowPriority.setImageResource(0);
binding.redPriority.setImageResource(0);
priority = "1";
Toast.makeText(UpdateNoteActivity.this, "You select a Low
Priority Navigation", Toast.LENGTH_SHORT).show();
}
});
binding.yellowPriority.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.yellowPriority.setImageResource(R.drawable.ic_done);
binding.greenPriority.setImageResource(0);
binding.redPriority.setImageResource(0);
priority = "2";
Toast.makeText(UpdateNoteActivity.this, "You select a Medium
Priority Navigation", Toast.LENGTH_SHORT).show();
}
});
binding.redPriority.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.redPriority.setImageResource(R.drawable.ic_done);
binding.greenPriority.setImageResource(0);
binding.yellowPriority.setImageResource(0);
priority = "3";
Toast.makeText(UpdateNoteActivity.this, "You select a High
Priority Navigation", Toast.LENGTH_SHORT).show();
}
});
binding.updateNotesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = binding.updateTitile.getText().toString();
String subtitle = binding.updateSubtitile.getText().toString();
String notes = binding.updateNote.getText().toString();
notesViewModel.updateNote(updatenote);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.delete_menu, menu);
return true;
}
@SuppressLint("MissingInflatedId")
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == R.id.delete)
{
BottomSheetDialog sheetDialog = new
BottomSheetDialog(UpdateNoteActivity.this, R.style.BottomSheetStyle);
sheetDialog.setContentView(view);
yes = view.findViewById(R.id.delete_yes);
no = view.findViewById(R.id.delete_no);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notesViewModel.deleteNote(sid);
finish();
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sheetDialog.dismiss();
}
});
sheetDialog.show();
}
return true;
}
}
-Lớp NotesAdapter
package com.trodev.myethicnotes.adapter;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.trodev.myethicnotes.Model.Notes;
import com.trodev.myethicnotes.R;
import com.trodev.myethicnotes.activities.MainActivity;
import com.trodev.myethicnotes.activities.UpdateNoteActivity;
import java.util.ArrayList;
import java.util.List;
@NonNull
@Override
public notesViewholder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
return new
notesViewholder(LayoutInflater.from(mainActivity).inflate(R.layout.item_notes,
parent, false));
}
@Override
public void onBindViewHolder(@NonNull notesViewholder holder, int
position) {
if (note.notesPriority.equals("1")) {
holder.notesPriority.setBackgroundResource(R.drawable.green_shape);
} else if (note.notesPriority.equals("2")) {
holder.notesPriority.setBackgroundResource(R.drawable.yellow_shape);
} else if (note.notesPriority.equals("3")) {
holder.notesPriority.setBackgroundResource(R.drawable.red_shape);
}
holder.noteTitle.setText(note.notesTitle);
holder.noteSubtitle.setText(note.notesSubTitle);
holder.noteDate.setText(note.notesDate);
@Override
public int getItemCount() {
return notes.size();
}
View notesPriority;
TextView noteTitle, noteSubtitle, noteDate;
public notesViewholder(@NonNull View itemView) {
super(itemView);
noteTitle = itemView.findViewById(R.id.notesTitle);
noteSubtitle = itemView.findViewById(R.id.noteSubtitle);
noteDate = itemView.findViewById(R.id.notesDate);
notesPriority = itemView.findViewById(R.id.notesPriority);
}
}
}
Trong adapter NotesAdapter, nhiệm vụ chính là hiển thị danh sách các ghi chú trong
RecyclerView . Dưới đây là giải thích về mã này:
1. Constructor và Dữ liệu: Adapter nhận một danh sách các đối tượng Notes và hoạt động
chính là hiển thị chúng trong RecyclerView. Adapter cũng nhận tham chiếu tới hoạt động
MainActivity.
2. Phương thức onCreateViewHolder(): Trong phương thức này, layout của mỗi item trong
RecyclerView được inflate từ tệp item_notes.xml, và được trả về dưới dạng một đối tượng
notesViewholder .
3. Phương thức onBindViewHolder() : Trong phương thức này, dữ liệu từ mỗi đối tượng
Notes trong danh sách được hiển thị trong RecyclerView . Các thông tin như tiêu đề, phụ
đề, ngày và mức độ ưu tiên được hiển thị trên các TextView. Màu nền của TextView hiển
thị mức độ ưu tiên được thiết lập dựa trên giá trị của notesPriority.
4. Xử lý sự kiện click: Khi người dùng click vào một item trong danh sách, một Intent
được tạo để chuyển đến UpdateNoteActivity, và dữ liệu về ghi chú tương ứng được chuyển
đi qua Intent để cập nhật.
5. Phương thức getItemCount(): Trả về số lượng các item trong danh sách ghi chú.
6. Class notesViewholder: Định nghĩa một lớp nội tuyến để đại diện cho mỗi viewholder
trong RecyclerView. Các thành phần giao diện như tiêu đề, phụ đề, ngày và mức độ ưu
tiên của ghi chú được tham chiếu và cập nhật trong lớp này.
Tóm lại, NotesAdapter là một adapter dùng để hiển thị danh sách các ghi chú trong RecyclerView,
và cung cấp chức năng click để chuyển đến màn hình cập nhật ghi chú khi người dùng click vào
một ghi chú.
\
1. Đây là giao diện thêm note
- EditText gồm Title và Subtitle để viết lại tiêu đề và tiêu đề con cho
notes.
- Textview Low,Medium,High để có thể đánh dấu mức độ quan trọng
của notes mà người sử dụng muốn lưu ý.
- EditText notesData để viết nội dung chính mà note này cần nhập.
2. Giao diện chính khi xuất hiện
- Có header là tên của chương trình
- TextView :
+ No Filter : để hiển thị các note một cách bình thường và không có sự sắp xếp.
+ High to Low: sắp xếp mức độ quan trọng của note theo thứ tự từ cao đến thấp.
+ Low to High : sắp xếp mức độ quan trọng của note theo thứ tự từ thấp đến cao.
- RecycleView : để hiện thị một danh sách các note
- FloatingActionButton: để khi nhấn vào sẽ xuất hiện ra một activity mới để có thể
thêm được các note mới.
3. Giao diện sau khi lưu một notes thành công
- Icon của FloatingActionButton từ dấu cộng thành hình điện thoại lưu xuống.
£