You are on page 1of 25

BÀI KIỂM TRA CUỐI

Họ và tên: Ngo Thị Nhi – Mã SV: 10121891

Lớp: 125216

BÀI LÀM

Bài 1: (TH số 06 )

 Code giao diện:

<?xml version="1.0" encoding="utf-8"?>


<TableLayout 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=".MainActivity">
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BÀI 1_TH6_NTNHi"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#B71C1C"
android:layout_gravity="center"/>
</TableRow>

<TableRow
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<EditText
android:id="@+id/edtHoten"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Nhập bản ghi... "
android:textSize="20sp"/>
</TableRow>

<TableRow
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>

<TableRow
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:textSize="18sp"
android:textStyle="bold"/>
</TableRow>

<TableRow
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbarSize="18sp"/>
</TableRow>

</TableLayout>

 Code chức năng:

 package com.example.th6_android;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


//Khai báo các biến giao diện
EditText edtHoTen;
Button btnAdd, btnDelete;
ListView listView;

ArrayList<String> arrayList; //Khai báo mảng dữ liệu


ArrayAdapter<String> adapter;
SQLiteDatabase database; //Khai báo đối tượng CSDL

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Tham chiếu cho các biến giao diện
edtHoTen = (EditText) findViewById(R.id.edtHoten);
btnAdd =(Button) findViewById(R.id.btnAdd);
btnDelete =(Button) findViewById(R.id.btnDelete);
listView = (ListView) findViewById(R.id.listview);

arrayList = new ArrayList<>(); //Tạo mới mảng dữ liệu

//Tạo mới adapter


adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter); //Đưa adapter lên listview

//Tạo và mở CSDL
database = openOrCreateDatabase("bai1thso06.db", MODE_PRIVATE,
null);

//Tạo Table để chứa dữ liệu


try {
String sql = "CREATE TABLE Detail(Name TEXT PRIMARY KEY)";
database.execSQL(sql);
}catch (Exception e){
Log.e("Lỗi!!!","Table đã tồn tại");
}
// Khi nhấp vào một mục trong ListView, hiển thị lên EditText
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selectedName = arrayList.get(position);
edtHoTen.setText(selectedName);
}
});

//------CHỨC NĂNG THÊM MỚI------


btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Name = edtHoTen.getText().toString();
ContentValues myvalues = new ContentValues();
myvalues.put("Name", Name);
String msq = "";
if (database.insert("Detail", null, myvalues) == -1)
{
msq = "Bản ghi này đã tồn tại!!!";
}
else {
msq = "Thêm bản ghi thành công!!!";
showLisView();
edtHoTen.setText("");//Đặt lại trường nhập về trạng
thái trống
}
Toast.makeText(MainActivity.this, msq,
Toast.LENGTH_SHORT).show();
}
});

//---------CHỨC NĂNG XÓA BẢN GHI ĐẦU TIÊN-------------


btnDelete.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
// Lấy bản ghi đầu tiên trong ListView
String FirstName = arrayList.get(0);
int n = database.delete("Detail", "Name = ?", new
String[]{FirstName});
String msq = "";
if (n == 0){
msq = "Không có bản ghi nào để xóa!!!";
}else{
msq = "Bản ghi đầu tiên đã được xóa thành công!!!";
showLisView();
}
Toast.makeText(MainActivity.this, msq,
Toast.LENGTH_SHORT).show();
}
});

private void showLisView(){


Cursor cursor = database.rawQuery("SELECT * FROM Detail",null);
arrayList.clear();
while(cursor.moveToNext()){
arrayList.add(cursor.getString(0));
}
adapter.notifyDataSetChanged();
}
}

- Kết quả của chức năng: Add New


- Kết quả của chức năng: Delete First
Bài 2: (TH số 06 )

 Code giao diện:

<?xml version="1.0" encoding="utf-8"?>


<TableLayout 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=".TH6_Bai2Activity">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="10dp">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BÀI 2_TH6_NTNHI"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="#B71C1C"
/>
</TableRow>

<TableRow
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<EditText
android:id="@+id/edtInputCSDL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Nhập tên CSDL... "
android:textSize="20sp"
android:ems="14"
android:inputType="text"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="6dp">

<Button
android:id="@+id/btnThemMoi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Thêm Mới"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#FFF"/>
</TableRow>

</TableLayout>

 Code chức năng:

package com.example.th6_android;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;

public class TH6_Bai2Activity extends AppCompatActivity {


EditText edtInputCSDL;
Button btnThemMoi;
SQLiteDatabase database;//Khai báo đối tượng CSDL

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_th6_bai2);
edtInputCSDL = (EditText) findViewById(R.id.edtInputCSDL);
btnThemMoi = (Button) findViewById(R.id.btnThemMoi);

//------ĐẶT SỰ KIỆN CHO NÚT "Thêm Mới"-------


btnThemMoi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Lấy tên của CSDL từ edt
String databaseName =
edtInputCSDL.getText().toString().trim();

//Ktra xem đã nhập tên CSDL vào edt hay chưa


if(!databaseName.isEmpty()){

// Kiểm tra xem CSDL đã tồn tại hay chưa


if(!checkDatabaseExists(databaseName )) {
//Nếu chưa tồn tại: mở hoặc tạo mới CSDL
database=openOrCreateDatabase(databaseName
,MODE_PRIVATE,null);
Toast.makeText(TH6_Bai2Activity.this, "CSDL có tên " +
databaseName + " đã được tạo thành công!!!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(TH6_Bai2Activity.this, "Lỗi!!! CSDL có
tên " + databaseName + " này đã tồn tại!!!", Toast.LENGTH_SHORT).show();
}
}else {
//Nếu chưa nhập tên CSDL ở edt
Toast.makeText(TH6_Bai2Activity.this, "Vui lòng nhập tên
CSDL muốn thêm!!!", Toast.LENGTH_SHORT).show();
}
}

});
}

// Hàm kiểm tra xem CSDL đã tồn tại hay chưa


private boolean checkDatabaseExists(String db) {
String dbPath = TH6_Bai2Activity.this.getDatabasePath(db).getPath();
File dbFile = new File(dbPath);
return dbFile.exists();
}

- Kết quả khi thêm CSDL thành công:


- Kết quả khi thêm CSDL thất bại:
Bài 4.1: (TH số 04)

 Code giao diện:

<?xml version="1.0" encoding="utf-8"?>


<TableLayout 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=".B41_TH4_Activity">
<TableRow
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginTop="10dp">

<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BÀI 4.1_TH4_NTNHI"
android:textStyle="bold"
android:textSize="25sp"
android:layout_gravity="center"
android:textColor="#C62828"/>
</TableRow>

<TableRow
android:layout_marginTop="15dp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:background="#7CB342">

<Spinner
android:id="@+id/spnCountry"
android:layout_width="match_parent"
android:layout_height="40dp"
android:prompt="@string/prompt_country"
android:scrollbarSize="22sp"
tools:ignore="TouchTargetSizeCheck" />

</TableRow>

<TableRow
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginTop="15dp">
<Button
android:id="@+id/btnSubmit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:text="Submit"
android:textStyle="bold"
android:textSize="18sp"
/>

</TableRow>

<TableRow
android:layout_marginTop="25dp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center">

<EditText
android:id="@+id/edtInputClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Nhập tên lớp..."
android:textSize="20sp"
android:textColor="#000"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="5dp">
<Button
android:id="@+id/btnThemClass"
android:layout_gravity="center"
android:text="Thêm Lớp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#FFF"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<ListView
android:id="@+id/listViewClass"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbarSize="18sp" />
</TableRow>

</TableLayout>
 Code chức năng:

package com.example.th6_android;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class B41_TH4_Activity extends AppCompatActivity {


Spinner spnCountry, spnList;
Button btnSubmit;
EditText edtInputClass;
Button btnThemClass;
ListView listViewClass;

ArrayAdapter<String> CountryAdapter;
List<String> CountryList;
ArrayAdapter<String> ClassAdapter;
List<String> ClassList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b41_th4);

//Ánh xạ sang các biến giao diện:


spnCountry = findViewById(R.id.spnCountry);
btnSubmit = findViewById(R.id.btnSubmit);
edtInputClass = findViewById(R.id.edtInputClass);
btnThemClass = findViewById(R.id.btnThemClass);
listViewClass = findViewById(R.id.listViewClass);

//Khởi tạo danh sách các nước


CountryList = new ArrayList<>(Arrays.asList("Malaysia", "United
States", "Indonesia", "France"));

//Khởi tạo danh sách các lớp học


ClassList = new ArrayList<>();

//Khởi tạo Adapter cho Spinner spnCountry v listview listViewClass


CountryAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, CountryList);
ClassAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, ClassList);

//Đặt Adapter cho spinner spnCountry và listView listViewClass


spnCountry.setAdapter(CountryAdapter);
listViewClass.setAdapter(ClassAdapter);

//Xử lý sự kiện khi lựa chọn 1 item trong Spinner spnCountry


spnCountry.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//Hiển thị giá trị được chọn trong AlertDialog
String selectedCountry =
spnCountry.getSelectedItem().toString();
showAlertDialog("Đã chọn Quốc gia", selectedCountry);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

//Xử lý sự kiện khi nhấn vào button: btnSubmit


btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Xóa giá trị được chọn khỏi spnCountry
int selectedPosition =
spnCountry.getSelectedItemPosition();
if (selectedPosition != Spinner.INVALID_POSITION){
CountryList.remove(selectedPosition);
CountryAdapter.notifyDataSetChanged();
}
}
});

//Xử lý sự kiện khi nhấn vào button: btnThemClass


btnThemClass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Lấy tên lớp từ edtInputClass
String className =
edtInputClass.getText().toString().trim();

// Kiểm tra xem edtInputClass có dữ liệu hay không


if (!className.isEmpty()) {
// Kiểm tra xem lớp đã tồn tại trong danh sách hay
chưa
if (!isClassExist(className)) {
// Nếu lớp chưa tồn tại, thêm lớp vào danh sách và
cập nhật listViewClass
ClassList.add(className);
ClassAdapter.notifyDataSetChanged();
// Hiển thị thông báo trong AlertDialog
showAlertDialog("Đã thêm lớp", "Lớp '" + className
+ "' đã được thêm");

// Sau khi thêm, xóa nội dung trong edtInputClass


edtInputClass.setText("");
} else {
// Nếu lớp đã tồn tại, hiển thị thông báo tương
ứng
showAlertDialog("Lỗi", "Lớp '" + className + "' đã
tồn tại trong danh sách");
}
}
}

private boolean isClassExist(String className) {


return ClassList.contains(className);
}
});

//Phương thức hiển thị AlertDialog:


private void showAlertDialog(String title, String message) {
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(this);
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage(message);
alertDialogBuilder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialogBuilder.create().show();
}

}
- Kết quả của trương trình Khi lựa chọn một Item trong Spinner thì hiển
thị giá trị lên màn hình (AlertDialog):
- Kết quả của chương trình sau khi lựa chọn một Item trong spnCountry và
nhấn btnSubmit thì xóa giá trị được chọn khỏi spnCountry: (Trong ví dụ này
chon item Malaysia States và nhấn btnSubmit)
- Kết quả của chương trình sau khi nhập tên lớp vào edtInputClass và click
btnThemClass trong listViewClass thì lớp tự động thêm vào listViewClass và
được hiển thị ra màn hình kèm thông báo AlertDialog:

 Khi tên lớp chưa tồn tại trong listViewClass thì thêm thành công


 Khi tên lớp đã tồn tại trong listViewClass thì thêm thất bại
Bài 4.2: (TH số 04)

 Code giao diện:

<?xml version="1.0" encoding="utf-8"?>


<TableLayout 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=".Bai42_TH4_Activity">
<TableRow
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginTop="10dp">

<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BÀI 4.2_TH4_NTNHI"
android:textStyle="bold"
android:textSize="25sp"
android:layout_gravity="center"
android:textColor="#C62828"/>
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="15dp">

<ListView
android:id="@+id/lvFruits"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbarSize="18sp" />
</TableRow>

<TableRow
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btnXoa"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:text="Xóa"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#FFF"
/>

</TableRow>

</TableLayout>

 Code chức năng:

package com.example.th6_android;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

public class Bai42_TH4_Activity extends AppCompatActivity {


ArrayList<String> arrFruits;
ListView lvFruits;
Button btnXoa;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bai42_th4);
//Ánh xạ các biến giao diện
lvFruits = findViewById(R.id.lvFruits);
btnXoa = findViewById(R.id.btnXoa);

//Khởi tạo danh sách Fruits


arrFruits = new ArrayList<>(Arrays.asList(
"Coconut",
"Durian",
"Guava",
"Kiwi fruit",
"Jack fruit",
"Mango",
"Olive"
));

adapter = new ArrayAdapter<String>(this,


android.R.layout.simple_list_item_1, arrFruits);
lvFruits.setAdapter(adapter);
BtnXoaOnClick();
}

private void BtnXoaOnClick() {


lvFruits.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
int i = position;
Toast.makeText(Bai42_TH4_Activity.this, arrFruits.get(i),
Toast.LENGTH_SHORT).show();

btnXoa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (arrFruits.size() > 0) {
arrFruits.remove(i);
lvFruits.setAdapter(adapter);
} else
Toast.makeText(Bai42_TH4_Activity.this, "Đã
hết quả để xóa rồi!!!", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {

}
}
});
}
});
}

}
- Khi lựa chọn một Item trong lvFruits thì hiển thị tên trái cây được chọn
lên màn hình (Alert Dialog).
- Kết quả sau khi chọn 1 item và nhấn vào btnXoa thì loại trái cây tương
ứng sẽ bị xóa ra khỏi lvFruits (Chọn item có tên Coconut và nhấn btnXoa thì
item được chọn đã được xóa ra khỏi lvFruits)
- Nếu như trong lvFruits đã hết các loại quả mà vẫn nhấn btnXoa thì sẽ
hiển thị thông báo “Đã hết quả để xóa rồi!!!”

You might also like