Vibe Coding Android: Tạo Ứng Dụng Ghi Chú Nhanh Bằng Java Trong 20 Phút

Sau thành công của ứng dụng “Bé Học Toán”, hôm nay chúng ta sẽ thử sức với một dự án phức tạp hơn: ứng dụng ghi chú nhanh cho Android. Với Vibe Coding, mọi thứ vẫn diễn ra nhanh chóng và đầy ngạc nhiên!

Mục tiêu: Tạo ứng dụng ghi chú cho phép thêm mới, xem danh sách và xóa ghi chú, sử dụng SQLite database.

Hành Trình 20 Phút “Vibe” Ra Ứng Dụng Ghi Chú

Bước 1: Tạo Model và Database Helper

Prompt 1 trong Cursor:

“Hãy tạo class Note.java làm model cho ghi chú, với các trường: id (int), title (String), content (String), timestamp (long). Và tạo DatabaseHelper.java extends SQLiteOpenHelper để quản lý database với bảng notes.”

Kết quả – File Note.java:

package com.tiilaz.easynoter;

/**
 * Simple model representing a note entry persisted in the local database.
 */
public class Note {

    private int id;
    private String title;
    private String content;
    private long timestamp;

    public Note() {
    }

    public Note(int id, String title, String content, long timestamp) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.timestamp = timestamp;
    }

    public Note(String title, String content, long timestamp) {
        this.title = title;
        this.content = content;
        this.timestamp = timestamp;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

Kết quả – File DatabaseHelper.java:

package com.tiilaz.easynoter;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Handles creation and upgrade of the notes SQLite database.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "easynoter.db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_NOTES = "notes";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_CONTENT = "content";
    public static final String COLUMN_TIMESTAMP = "timestamp";

    private static final String CREATE_TABLE_NOTES = "CREATE TABLE " + TABLE_NOTES + " ("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_TITLE + " TEXT NOT NULL, "
            + COLUMN_CONTENT + " TEXT, "
            + COLUMN_TIMESTAMP + " INTEGER NOT NULL"
            + ");";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_NOTES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTES);
        onCreate(db);
    }
}

Bước 2: Tạo Giao Diện Chính

Prompt 2:

“Tạo activity_main.xml cho ứng dụng ghi chú: có FloatingActionButton để thêm ghi chú mới, RecyclerView để hiển thị danh sách ghi chú, và TextView thông báo khi không có ghi chú.”

Kết quả – File activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerNotes"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:clipToPadding="false"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/item_note" />

    <TextView
        android:id="@+id/textEmptyState"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chưa có ghi chú nào."
        android:textAppearance="@style/TextAppearance.Material3.TitleMedium"
        android:textColor="@android:color/darker_gray"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabAddNote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintVertical_bias="1"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintBottom_margin="24dp"
        app:layout_constraintEnd_margin="24dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Bước 3: Tạo Adapter cho RecyclerView

Prompt 3:

“Tạo NoteAdapter.java cho RecyclerView để hiển thị danh sách ghi chú. Mỗi item hiển thị title, content rút gọn (tối đa 50 ký tự), và timestamp định dạng ngày tháng. Có nút xóa ở mỗi item.”

Kết quả tạo ra-
(1) NoteAdapter.java:

package com.tiilaz.easynoter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Adapter responsible for rendering the list of notes in the RecyclerView.
 */
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteViewHolder> {

    private static final int CONTENT_PREVIEW_LIMIT = 50;

    private final List<Note> notes = new ArrayList<>();
    private final OnNoteDeleteListener deleteListener;
    private final SimpleDateFormat dateFormat =
            new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());

    public interface OnNoteDeleteListener {
        void onDelete(Note note);
    }

    public NoteAdapter(OnNoteDeleteListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    public void setNotes(List<Note> newNotes) {
        notes.clear();
        if (newNotes != null) {
            notes.addAll(newNotes);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_note, parent, false);
        return new NoteViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
        Note note = notes.get(position);
        holder.bind(note);
    }

    @Override
    public int getItemCount() {
        return notes.size();
    }

    private String getPreviewContent(String content) {
        if (content == null) {
            return "";
        }
        String trimmed = content.trim();
        if (trimmed.length() <= CONTENT_PREVIEW_LIMIT) {
            return trimmed;
        }
        return trimmed.substring(0, CONTENT_PREVIEW_LIMIT) + "...";
    }

    private String formatTimestamp(long timestamp) {
        return dateFormat.format(new Date(timestamp));
    }

    class NoteViewHolder extends RecyclerView.ViewHolder {

        private final TextView textTitle;
        private final TextView textContent;
        private final TextView textTimestamp;
        private final ImageButton buttonDelete;

        NoteViewHolder(@NonNull View itemView) {
            super(itemView);
            textTitle = itemView.findViewById(R.id.textTitle);
            textContent = itemView.findViewById(R.id.textContent);
            textTimestamp = itemView.findViewById(R.id.textTimestamp);
            buttonDelete = itemView.findViewById(R.id.buttonDelete);
        }

        void bind(Note note) {
            textTitle.setText(note.getTitle());
            textContent.setText(getPreviewContent(note.getContent()));
            textTimestamp.setText(formatTimestamp(note.getTimestamp()));
            buttonDelete.setOnClickListener(v -> {
                if (deleteListener != null) {
                    deleteListener.onDelete(note);
                }
            });
        }
    }
}

(2) item_note.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorSurface"
    android:padding="16dp">

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.Material3.TitleMedium"
        android:textColor="?android:attr/textColorPrimary"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/buttonDelete"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Meeting notes" />

    <ImageButton
        android:id="@+id/buttonDelete"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:contentDescription="@string/delete"
        android:padding="8dp"
        android:src="@android:drawable/ic_menu_delete"
        android:tint="?attr/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textContent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:maxLines="2"
        android:ellipsize="end"
        android:textAppearance="@style/TextAppearance.Material3.BodyMedium"
        android:textColor="?android:attr/textColorSecondary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textTitle"
        tools:text="Here is a preview of the note content limited to two lines." />

    <TextView
        android:id="@+id/textTimestamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textAppearance="@style/TextAppearance.Material3.LabelSmall"
        android:textColor="?android:attr/textColorSecondary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textContent"
        tools:text="24/11/2025 08:30" />

</androidx.constraintlayout.widget.ConstraintLayout>

Bước 4: Tạo MainActivity và Logic Chính

Prompt 4:

“Tạo MainActivity.java kết nối tất cả lại: khởi tạo database, hiển thị danh sách ghi chú, xử lý thêm ghi chú mới bằng AlertDialog, và xóa ghi chú.”

Kết quả – File MainActivity.java:

package com.tiilaz.easynoter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private DatabaseHelper databaseHelper;
    private NoteAdapter noteAdapter;
    private RecyclerView recyclerNotes;
    private TextView textEmptyState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initData();
    }

    private void initViews() {
        recyclerNotes = findViewById(R.id.recyclerNotes);
        textEmptyState = findViewById(R.id.textEmptyState);
        FloatingActionButton fabAddNote = findViewById(R.id.fabAddNote);

        recyclerNotes.setLayoutManager(new LinearLayoutManager(this));
        noteAdapter = new NoteAdapter(this::deleteNote);
        recyclerNotes.setAdapter(noteAdapter);

        fabAddNote.setOnClickListener(v -> showAddNoteDialog());
    }

    private void initData() {
        databaseHelper = new DatabaseHelper(this);
        loadNotes();
    }

    private void loadNotes() {
        List<Note> notes = databaseHelper.getAllNotes();
        noteAdapter.setNotes(notes);
        boolean isEmpty = notes == null || notes.isEmpty();
        textEmptyState.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
        recyclerNotes.setVisibility(isEmpty ? View.GONE : View.VISIBLE);
    }

    private void showAddNoteDialog() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_note, null);
        EditText editTitle = dialogView.findViewById(R.id.editTitle);
        EditText editContent = dialogView.findViewById(R.id.editContent);

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle(R.string.add_note)
                .setView(dialogView)
                .setPositiveButton(R.string.save, null)
                .setNegativeButton(R.string.cancel, (d, w) -> d.dismiss())
                .create();

        dialog.setOnShowListener(d -> {
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
                String title = editTitle.getText().toString().trim();
                String content = editContent.getText().toString().trim();

                if (title.isEmpty()) {
                    editTitle.setError(getString(R.string.title_required));
                    editTitle.requestFocus();
                    return;
                }

                Note note = new Note(title, content, System.currentTimeMillis());
                databaseHelper.insertNote(note);
                Toast.makeText(this, R.string.note_saved, Toast.LENGTH_SHORT).show();
                loadNotes();
                dialog.dismiss();
            });
        });

        dialog.show();
    }

    private void deleteNote(Note note) {
        databaseHelper.deleteNote(note.getId());
        Toast.makeText(this, R.string.note_deleted, Toast.LENGTH_SHORT).show();
        loadNotes();
    }
}

Điều Đáng Ngạc Nhiên Từ Thực Nghiệm

  1. AI Hiểu Cả Kiến Trức Ứng Dụng: Từ database design đến UI/UX, AI đề xuất giải pháp tổng thể khá hợp lý.
  2. Code Chất Lượng Cao: Code sinh ra có đầy đủ error handling, comment, và tuân thủ best practices.
  3. Tiết Kiệm Thời Gian Khổng Lồ: Thay vì mất 2-3 giờ code thủ công, giờ chỉ cần 20 phút “chỉ đạo” AI.
  4. Học Hỏi Từ Code AI: Ngay cả developer kinh nghiệm cũng có thể học được các kỹ thuật mới từ code mà AI sinh ra.

Kết Luận: Vibe Coding – Không Chỉ Là Prototyping

Ứng dụng ghi chú này chứng minh Vibe Coding có thể tạo ra sản phẩm thực tế, có thể sử dụng ngay. Đây không còn là “toy project” mà là ứng dụng hoàn chỉnh với database, UI và đầy đủ tính năng CRUD.

Comments are closed.