Thêm Dữ Liệu Vào SQLite Android Làm App Todo - Jundat95

Jundat95

System.out.print('Hello world!');

Header Ads

  • Home
    • Knowledge
    • _Android
    • _React Native
    • _IOS
    • _Java
    • _JavaScript
    • _C#
    • _HTML
    • Operating system
    • _Windows
    • _Ubuntu
    • Tutorial
    • Tools
    • Ebook
    Home Android Học Tập SQLite Thêm dữ liệu vào SQLite Android làm app Todo Thêm dữ liệu vào SQLite Android làm app Todo Android, Học Tập, SQLite

    Thao tác thêm dữ liệu vào SQLite ứng dụng viết app Todo List

    sqlite add

    Ở bài trước Click chúng ta đã xây dựng xong phần SQLiteHelper giúp thao tác với database được lưu trong SQLite, ở đó có hai phương thức chính là lấy ra danh sách todo và thêm một trường dữ liệu vào database, hôm nay chúng ta sẽ làm chức năng thêm dữ liệu thông qua giao diện android.

    Yêu cầu bài là đã học bài trước. Oke, giờ chúng ta bắt tay vào xây dựng chức năng Add Todo

    Đầu tiên chúng ta cần tạo một Activity là AddActivity để thực hiện chức năng thêm dữ liệu vào DB

    Các bạn thêm package mới vào phần view -> sau đó tạo AddActivity vào trong đó sqlite add

    Sau đó chúng ta sẽ thiết kế giao diện cho nó, giao diện bao gồm TextView và EditText

    layout/activity_add.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="match_parent" tools:context="com.jundat95.todoapp.view.add.AddActivity"> <TextView android:id="@+id/tvHeader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="24dp" android:text="Add Todo" android:textSize="18sp" /> <EditText android:id="@+id/edtDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:ems="10" android:hint="Date" android:inputType="textPersonName" /> <EditText android:id="@+id/edtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/edtDate" android:layout_alignParentEnd="true" android:layout_alignParentStart="true" android:layout_marginBottom="11dp" android:ems="10" android:hint="Title" android:inputType="textPersonName" /> <EditText android:id="@+id/edtContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentStart="true" android:layout_below="@+id/edtDate" android:layout_marginTop="14dp" android:ems="10" android:hint="Content" android:inputType="textPersonName" /> <Button android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="14dp"

    Tiếp theo chúng ta sẽ sang AddActivity ánh xạ và thao tác thêm dữ liệu vào DB: App Todo sử dụng SQLite

    package com.jundat95.todoapp.view.add; import android.content.ContentValues; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.jundat95.todoapp.R; import com.jundat95.todoapp.sqlite.SQLiteHelper; import java.util.Date; public class AddActivity extends AppCompatActivity { private Button btnAdd; private EditText edtTitle, edtDate, edtContent; private SQLiteHelper sqLiteHelper = new SQLiteHelper(this, 2); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); init(); } private void init() { edtTitle = (EditText) findViewById(R.id.edtTitle); edtDate = (EditText) findViewById(R.id.edtDate); edtContent = (EditText) findViewById(R.id.edtContent); btnAdd = (Button) findViewById(R.id.btnAdd); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { addTodo(); } }); } private void addTodo() { // Tạo ra một Id random theo thoi gian long time = new Date().getTime(); // Su dung contentValues để lưu dữ liệu, sau đó truyền vào SQLiteHelper ContentValues contentValues = new ContentValues(); // Truyền vào contentValues dữ liệu có dạng [key, value] // Key là tên cột ở trong bảng, Value là giá trị muốn thêm vào cột đó contentValues.put("Id", time); contentValues.put("Title", edtTitle.getText().toString()); contentValues.put("Date", edtDate.getText().toString()); contentValues.put("Content", edtContent.getText().toString()); // sử dụng phương thức addToDo với tham số là contentValues long n = sqLiteHelper.addToDo(contentValues); // n > 0 là thêm được dữ liệu vào trong bảng if(n > 0) { Toast.makeText(this, "Add complete", Toast.LENGTH_SHORT).show(); // Destroy activity add finish(); } } }

    Lưu ý sau khi thêm dữ liệu app báo thành công chúng ta thoát app ra vào lại sẽhiện thị dữ liệu mới thêm lên.

    Like và share để ủng hộ mình nhé :D

    Related Posts

    SQLite

    Post a Comment

    No comments

    Subscribe to: Post Comments ( Atom )

    Translate

    STAY WITH US

    • 114 followers
    • 250 followers
    • 500 likes
    • 0 followers
    • 1000 subscribers
    • 266 followers

    Facebook

    Popular Posts

    •  Hướng dẫn cách xóa thay đổi commit trên remote github Hướng dẫn cách xóa thay đổi commit trên remote github  Hướng dẫn cách xóa thay đổi commit trên remote github Vào một ngày đẹp trời khi bạn đã trưởng thành :)) bạn bật chiếc máy tính thân yêu của...
    • Bài tập quản lý Sinh Viên SQL Bài tập quản lý Sinh Viên SQL Bài tập quản lý Sinh Viên SQL   CREATE DATABASE QLSV USE QLSV --Tạo bảng Lớp CREATE TABLE tblLOP ( MaLop varchar (10) PRIMARY ...
    • Hướng dẫn cấu hình nginx làm proxy cho docker, apache, magento 2 Hướng dẫn cấu hình nginx làm proxy cho docker, apache, magento 2 Hướng dẫn cấu hình nginx làm proxy cho docker, apache, magento 2 Giải sử bạn có một site magento 2 chạy trên magento-docker (apache)...
    • Bài Tập Quản Lý Thư Viện SQL (Bài 5) Bài Tập Quản Lý Thư Viện SQL (Bài 5) Bài Tập Quản Lý Thư Viện SQL (Bài 5)    Code: Tải về Code use QuanLyThuVien -- Quan Ly thu vien SQL by jundat95 -- View doc gia h...

    Arquivo do blog

    • ▼  2017 (35)
      • ▼  October (7)
        • Cách duyệt đồ thị theo chiều rộng Java
        • Duyệt đồ thị theo chiều sâu Java
        • Java fibonacci algorithm
        • Xử lý xự kiện bấm vào Listview trong app Todo
        • Custom ListView trong Android làm app Todo
        • Thêm dữ liệu vào SQLite Android làm app Todo
        • Hướng dẫn sử dụng SQLite làm app todo trong android

    Recent Posts

    Recent Comments

    Created By Tinh Ngo

    Từ khóa » Thêm Dữ Liệu Vào Sqlite