BÀI KIỂM TRA CUỐI ANDROID - NGÔ THỊ NHI
BÀI KIỂM TRA CUỐI ANDROID - NGÔ THỊ NHI
BÀI KIỂM TRA CUỐI ANDROID - NGÔ THỊ NHI
Lớp: 125216
BÀI LÀM
Bài 1: (TH số 06 )
<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>
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;
@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);
//Tạo và mở CSDL
database = openOrCreateDatabase("bai1thso06.db", MODE_PRIVATE,
null);
@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();
}
});
<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>
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;
@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);
});
}
<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;
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);
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
- 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)
<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>
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;
@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);
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!!!”