public class VocabularyActivity extends AppCompatActivity {
EditText edtmatu, edttentu, edtnghia, edtphienam, edtloaitu;
Button btninsert, btndelete, btnupdate,btnquery;
ListView lv;
ArrayList<String> mylist;
ArrayAdapter<String> myadapter;
SQLiteDatabase mydatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtmatu = findViewById(R.id.edtmatu);
edttentu = findViewById(R.id.edttentu);
edtnghia = findViewById(R.id.edtnghia);
edtphienam = findViewById(R.id.edtphienam);
edtloaitu = findViewById(R.id.edtloaitu);
btninsert = findViewById(R.id.btninsert);
btndelete = findViewById(R.id.btndelete);
btnupdate = findViewById(R.id.btnupdate);
btnquery = findViewById(R.id.btnquery);
lv = findViewById(R.id.lv);
mylist = new ArrayList<>();
myadapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,mylist);
lv.setAdapter(myadapter);
mydatabase = openOrCreateDatabase(“qltuvungtienganh”,MODE_PRIVATE,null);
try{
String sql = “CREATE TABLE tbltuvung(matu TEXT primary key,tentu TEXT,phienam TEXT,loaitu TEXT,nghia TEXT)”;
mydatabase.execSQL(sql);
}
catch(Exception e){
Log.e(“Error”,”Table đã tồn tại”);
}
btninsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String matu = edtmatu.getText().toString();
String tentu = edttentu.getText().toString();
String nghia = edtnghia.getText().toString();
String phienam = edtphienam.getText().toString();
String loaitu = edtloaitu.getText().toString();
ContentValues myvalue = new ContentValues();
myvalue.put(“matu”,matu);
myvalue.put(“tentu”,tentu);
myvalue.put(“nghia”,nghia);
myvalue.put(“phienam”,phienam);
myvalue.put(“loaitu”,loaitu);
String msg = “”;
if(mydatabase.insert(“tbltuvung”,null,myvalue)== -1){
msg = “Fail to Insert Record!”;
}else{
msg = “Insert record Sucessfully”;
}
Toast.makeText(VocabularyActivity.this,msg, Toast.LENGTH_SHORT).show();
}
});
btndelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String matu = edtmatu.getText().toString();
int n = mydatabase.delete(“tbltuvung”,”matu = ?”,new String[]{matu});
String msg =””;
if(n ==0){
msg = “No record to Delete”;
}
else{
msg = n+ ” record is deleted”;
}
Toast.makeText(VocabularyActivity.this,msg,Toast.LENGTH_SHORT).show();
}
});
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tentu = edttentu.getText().toString();
String nghia = edtnghia.getText().toString();
String phienam = edtphienam.getText().toString();
String loaitu = edtloaitu.getText().toString();
String matu = edtmatu.getText().toString();
ContentValues myvalue = new ContentValues();
myvalue.put(“tentu”,tentu);
myvalue.put(“nghia”,nghia);
myvalue.put(“phienam”,phienam);
myvalue.put(“loaitu”,loaitu);
int n = mydatabase.update(“tbltuvung”,myvalue,”matu = ?”,new String[]{matu});
String msg = “”;
if(n == 0){
msg = “No record to Update”;
}
else{
msg = n+ ” record is updated”;
}
Toast.makeText(VocabularyActivity.this,msg,Toast.LENGTH_SHORT).show();
}
});
btnquery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mylist.clear();
Cursor c = mydatabase.query(“tbltuvung”,null,null,null,null,null,null);
c.moveToNext();
String data = “”;
while(c.isAfterLast() == false){
data = c.getString(0)+”. ” + c.getString(1)+” /” + c.getString(2)+ “/ (“+c.getString(3)+”): ” + c.getString(4);
c.moveToNext();
mylist.add(data);
}
c.close();
myadapter.notifyDataSetChanged();
}
});
}
} |