Quản lý sinh viên: Bước 1: tạo giao diện: android app tools android android android tools

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Quản lý sinh viên:

Bước 1: tạo giao diện

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


<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/txtTen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="nhập tên sinh viên" />

<EditText
android:id="@+id/txtLop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="nhập lớp sinh viên" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">

<Button
android:id="@+id/btnThem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Thêm" />

<Button
android:id="@+id/btnHienThi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hiển Thị" />

</LinearLayout>
<ListView
android:id="@+id/lvDanhSach"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>

</LinearLayout>

Bước 2: xây dựng class SinhVien

public class SinhVien {


String id, ten, lop;

public String getId() {


return id;
}

public void setId(String id) {


this.id = id;
}

public String getTen() {


return ten;
}

public void setTen(String ten) {


this.ten = ten;
}

public String getLop() {


return lop;
}

public void setLop(String lop) {


this.lop = lop;
}

@Override
public String toString() {
return "SinhVien{" +
"id='" + id + '\'' +
", ten='" + ten + '\'' +
", lop='" + lop + '\'' +
'}';
}
}

Bước 3: xây dựng lớp DBHelper


public class DBHelper extends SQLiteOpenHelper {

// Tên cơ sở dữ liệu
public static final String TEN_DATABASE = "QuanLySinhVien";
// Tên bảng
public static final String TEN_BANG_SINHVIEN = "SinhVien";
// Bảng gồm 3 cột _id, _ten và _lop.
public static final String COT_ID = "_id";
public static final String COT_TEN = "_ten";
public static final String COT_LOP = "_lop";

private static final String TAO_BANG_SINHVIEN = ""


+ "create table " + TEN_BANG_SINHVIEN + " ( "
+ COT_ID + " integer primary key autoincrement ,"
+ COT_TEN + " text not null, "
+ COT_LOP + " text not null );";
public DBHelper( Context context) {
super(context,TEN_DATABASE,null,1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TAO_BANG_SINHVIEN);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

Bước 4: xây dựng lớp SinhVienDB


public class SinhVienDB {
SQLiteDatabase database;
DBHelper dbHelper;

public SinhVienDB(Context context) {


dbHelper = new DBHelper(context);
try {
database = dbHelper.getWritableDatabase();
} catch (SQLiteException ex) {
database = dbHelper.getReadableDatabase();
}

public void close() {


dbHelper.close();

public Cursor layTatCaDuLieu() {


// Biến cot là khai báo danh sách các cột cần lấy.
String[] cot = {DBHelper.COT_ID,
DBHelper.COT_TEN,
DBHelper.COT_LOP};
Cursor cursor = null;
cursor = database.query(DBHelper.
TEN_BANG_SINHVIEN, cot, null, null, null, null,
DBHelper.COT_ID + " DESC");
return cursor;
}

public long them(SinhVien sinhVien) {


ContentValues values = new ContentValues();
values.put(DBHelper.COT_TEN,
sinhVien.getTen());
values.put(DBHelper.COT_LOP,
sinhVien.getLop());
return database.insert(DBHelper.
TEN_BANG_SINHVIEN, null, values);
}

public long xoa(SinhVien sinhVien) {


return database.delete(DBHelper
.TEN_BANG_SINHVIEN, DBHelper
.COT_TEN + " = " + "'" +
sinhVien.getTen() + "'", null);
}

public long sua(SinhVien sinhVien) {


ContentValues values = new ContentValues();
values.put(DBHelper.COT_TEN,
sinhVien.getTen());
values.put(DBHelper.COT_LOP,
sinhVien.getLop());
return database.update(DBHelper
.TEN_BANG_SINHVIEN, values,
DBHelper.COT_ID + " = "
+ sinhVien.getId(), null);
}

Bước 5: code MainActivity

public class MainActivity extends AppCompatActivity {


EditText txtTen, txtLop;
Button btnThem, btnHienThi;
ListView lvDanhSach;

ArrayList<SinhVien> data = new ArrayList<>();


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

setControl();
setEvent();

private void setEvent() {


sinhVienDB = new SinhVienDB(this);

final ArrayAdapter<SinhVien> sinhVienArrayAdapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1,data);
lvDanhSach.setAdapter(sinhVienArrayAdapter);

btnThem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

SinhVien sv= layDL();


//data.add(sv);
//sinhVienArrayAdapter.notifyDataSetChanged();
sinhVienDB.them(sv);

}
});

btnHienThi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = sinhVienDB.layTatCaDuLieu();
if (cursor != null) {
data.clear();
while (cursor.moveToNext()) {
SinhVien sv = new SinhVien();
sv.setId("" + cursor.getInt(0));
sv.setTen(cursor.getString(1));
sv.setLop(cursor.getString(2));
data.add(sv);
}
sinhVienArrayAdapter.notifyDataSetChanged();
}
}
});
}

private SinhVien layDL()


{
SinhVien sinhVien = new SinhVien();
sinhVien.setTen(txtTen.getText().toString());
sinhVien.setLop(txtLop.getText().toString());
return sinhVien;
}

private void setControl() {


txtTen =findViewById(R.id.txtTen);
txtLop =findViewById(R.id.txtLop);

btnThem = findViewById(R.id.btnThem);
btnHienThi= findViewById(R.id.btnHienThi);
lvDanhSach =findViewById(R.id.lvDanhSach);

}
}

You might also like