You are on page 1of 9

Bài tập thực hành Lập trình thiết bị di động

LAB 08: Lưu trữ dữ liệu


MỤC TIÊU
• Tạo ứng dụng có sử dụng đối tượng SharedPreferences để lưu trữ dữ liệu ở dạng đơn giản.
• Sử dụng SQLite để tạo cơ sở dữ liệu cho ứng dụng.

NỘI DUNG
Ví dụ 1: cách sử dụng SharedPreferences:
Xây dựng ứng dụng cho phép người dùng nhập thông tin cá nhân và lưu
lại cho lần sau khi mở ứng dụng.

Hướng dẫn:
Bước 1: Tạo giao diện

Trang 1
Bài tập thực hành Lập trình thiết bị di động

Bước 2: Khai báo thư viện

Bước 3: Tạo Hàm ReadSP (dùng để đọc Shared Preferences)

Bước 4: Tạo Hàm WriteSP (dùng để ghi Shared Preferences)

Trang 2
Bài tập thực hành Lập trình thiết bị di động

Bước 5: Chèn code vào sự kiện onCreate của Activity

Bước 6: Chạy thử và sửa lỗi (nếu có)

Ví dụ 2: cách sử dụng SQLite


Tóm tắt lý thuyết:
• SQLite là cơ sở dữ liệu quan hệ tương thích với SQL.

Trang 3
Bài tập thực hành Lập trình thiết bị di động

• SQLite được tích hợp sẵn trên hệ điều hành Android.


• SQLite là cơ sở dữ liệu cục bộ (local) và không hỗ trợ truy cập dạng Server -
Client.
• Để sử dụng truy cập cơ sở dữ liệu SQLite và thực hiện các thao các tạo
table, thêm, xóa sửa dữ liệu chúng ta cần sử dụng class SQLiteOpenHelper

Bước 1: Thiết kế giao diện:

<?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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Trang 4
Bài tập thực hành Lập trình thiết bị di động

</RelativeLayout>

Bước 2: Tạo class với tên mySQLite.cs:

Bước 3: Viết code trong file MainActivity.cs:


using Android.App;
using Android.OS;
using Android.Runtime;
using AndroidX.AppCompat.App;
using Android.Widget;
using Android.Database;

namespace SQLiteSample
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher =
true)]
public class MainActivity : AppCompatActivity
{
TextView textView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
textView = FindViewById<TextView>(Resource.Id.textView);

// Tạo csdl SQLite:


mySQLite mysqlite = new mySQLite(this.ApplicationContext);

// Tạo table SINHVIEN:

Trang 5
Bài tập thực hành Lập trình thiết bị di động

string strCreate = "CREATE TABLE IF NOT EXISTS SINHVIEN (" +


"masv Integer IDENTITY(1,1) Primary Key, " +
"hoten Text)";
mysqlite.ReadableDatabase.ExecSQL(strCreate);

// Insert dữ liệu vào table SINHVIEN:


mysqlite.ReadableDatabase.ExecSQL("INSERT INTO SINHVIEN(hoten)
VALUES('Trần Văn Tâm')");
mysqlite.ReadableDatabase.ExecSQL("INSERT INTO SINHVIEN(hoten)
VALUES('Nguyễn Văn A')");
mysqlite.ReadableDatabase.ExecSQL("INSERT INTO SINHVIEN(hoten)
VALUES('Trương Văn Phong')");
mysqlite.ReadableDatabase.ExecSQL("INSERT INTO SINHVIEN(hoten)
VALUES('Bùi Anh Châu')");

// Đọc dữ liệu và hiển thị lên TextView:


ICursor cur;
cur = mysqlite.ReadableDatabase.RawQuery("SELECT * FROM SINHVIEN",null);
while(cur.MoveToNext())
{
textView.Text += cur.GetString(1) + "\n"; // 1: cột họ tên sinh viên
}
}

public override void OnRequestPermissionsResult(int requestCode, string[]


permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode,
permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);


}
}
}

Trang 6
Bài tập thực hành Lập trình thiết bị di động

Bước 4: Chạy thử chương trình và sửa lỗi (nếu có)

Trang 7
Bài tập thực hành Lập trình thiết bị di động

Bài tập:
Sử dụng Visual Studio tạo ứng dụng có dạng như sau:

YÊU CẦU

STT NỘI DUNG


1 - Thiết kế giao diện ứng dụng giao diện giống như mẫu
2 - Button Thêm: Thêm sản phẩm được nhập từ EditText vào danh sách phía
dưới.

3 - Button Xóa tất cả: Xóa tất cả những sản phẩm hiện có trong ListView
4 - Có chức năng kiểm tra sản phẩm đã tồn tại và không cho phép thêm vào danh
sách:

Trang 8
Bài tập thực hành Lập trình thiết bị di động

5 - Dữ liệu trên danh sách sẽ được lưu lại cho lần sau khi mở ứng dụng.

- HẾT -

Trang 9

You might also like