Thêm Dữ Liệu Vào SQLite Android Làm App Todo - Jundat95
Có thể bạn quan tâm
Jundat95 Home
Facebook
System.out.print('Hello world!');
Header Ads
- Knowledge
- _Android
- _React Native
- _IOS
- _Java
- _JavaScript
- _C#
- _HTML
- Operating system
- _Windows
- _Ubuntu
- Tutorial
- Tools
- Ebook
Thao tác thêm dữ liệu vào SQLite ứng dụng viết app Todo List
Ở 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 đó
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é :DRelated Posts
SQLitePost 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
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 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 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 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) Code: Tải về Code use QuanLyThuVien -- Quan Ly thu vien SQL by jundat95 -- View doc gia h...
Arquivo do blog
- ► 2024 (1)
- ► April (1)
- ► 2023 (1)
- ► August (1)
- ► 2022 (4)
- ► November (1)
- ► May (1)
- ► April (1)
- ► January (1)
- ► 2021 (26)
- ► December (6)
- ► September (3)
- ► August (1)
- ► July (1)
- ► May (3)
- ► April (3)
- ► March (3)
- ► February (2)
- ► January (4)
- ► 2020 (22)
- ► December (1)
- ► September (1)
- ► August (4)
- ► July (2)
- ► April (3)
- ► March (8)
- ► January (3)
- ► 2019 (42)
- ► December (2)
- ► November (3)
- ► October (2)
- ► September (2)
- ► July (5)
- ► May (3)
- ► April (10)
- ► March (7)
- ► February (7)
- ► January (1)
- ► 2018 (19)
- ► December (1)
- ► November (7)
- ► September (1)
- ► August (1)
- ► July (2)
- ► June (1)
- ► May (1)
- ► February (2)
- ► January (3)
- ► 2016 (15)
- ► December (1)
- ► October (2)
- ► September (1)
- ► August (3)
- ► July (2)
- ► June (1)
- ► May (2)
- ► April (3)
- ► 2015 (75)
- ► December (3)
- ► November (2)
- ► October (8)
- ► September (7)
- ► August (7)
- ► July (4)
- ► June (4)
- ► May (3)
- ► April (5)
- ► March (16)
- ► February (3)
- ► January (13)
- ► 2014 (12)
- ► December (6)
- ► November (2)
- ► September (3)
- ► August (1)
Recent Posts
Recent Comments
Created By Tinh NgoTừ khóa » Thêm Dữ Liệu Vào Sqlite
-
SQLite Và Cách Thêm Dữ Liệu - EDU.COM.VN
-
Lệnh INSERT Trong SQLite - Freetuts
-
Lưu Dữ Liệu Bằng SQLite | Android Developers
-
Lưu Trữ Dữ Liệu Với SQLite | How Kteam
-
Sử Dụng SQLite Database Trong ứng Dụng Android - Viblo
-
Sử Dụng SQLite Trong Android (phần 1)
-
Giới Thiệu SQLite Và Thực Hành Lệnh SQL
-
Android Sqlite Database P5 - Thêm Dữ Liệu Vào Bảng (1) - YouTube
-
Android: Lưu Dữ Liệu Bằng SQLite | V1Study
-
Lập Trình Android - Cơ Sở Dữ Liệu SQLite
-
Hướng Dẫn Lập Trình Android Với Database SQLite - Openplanning
-
Lưu Trữ Dữ Liệu Với SQLite Trong Android - STDIO
-
1. SQLite Database - Lập Trình Android Cơ Bản - VnCoder
-
Hướng Dẫn Kết Nối Cơ Sở Dữ Liệu Với Sqlite - Hoàn Vũ