ngày 23-08-2017
public class DBHelper { |
public class Student {
int id;
String name;
String address;
int gender;
public Student() {
}
public Student(int id, String name, String address, int gender) {
this.id = id;
this.name = name;
this.address = address;
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
|
<?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"
tools:context="com.example.vudinhai.bt_lab3.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"/>
</LinearLayout>
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Name"
android:id="@+id/txtName"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Name"
android:id="@+id/txtAddress"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
|
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
android:padding="16dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textSize="18sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtName"
android:textAlignment="center"
android:layout_span="2"
android:hint="name"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address:"
android:textSize="18sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtAddress"
android:textAlignment="center"
android:layout_span="2"
android:hint="address"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="center|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender:"
android:textSize="18sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:id="@+id/rdgGender">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rdbMale"
android:text="Male"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rdbFemale"
android:text="FeMale"/>
</RadioGroup>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"
android:src="@color/colorAccent" />
</TableRow>
</TableLayout>
|
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Add"
android:id="@+id/mnu_add"
android:icon="@drawable/ic_add_black_24dp"
app:showAsAction="always"/>
</menu>
|
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Update"
android:id="@+id/mnu_update" />
<item android:title="Delete"
android:id="@+id/mnu_delete" />
</menu>
|
public class StudentAdapter extends ArrayAdapter<Student> {
Context context;
int layout;
ArrayList<Student> arrayList;
public StudentAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Student> objects) {
super(context, resource, objects);
this.context = context;
this.layout = resource;
this.arrayList = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater =LayoutInflater.from(context);
convertView = inflater.inflate(layout,null);
ImageView img =(ImageView)convertView.findViewById(R.id.image);
TextView txt1 = (TextView)convertView.findViewById(R.id.txtName);
TextView txt2 = (TextView)convertView.findViewById(R.id.txtAddress);
if(arrayList.get(position).getGender() == 1) {
img.setImageResource(R.drawable.male);
}else {
img.setImageResource(R.drawable.female);
}
txt1.setText(arrayList.get(position).getName());
txt2.setText(arrayList.get(position).getAddress());
return convertView;
}
}
|
public class MainActivity extends AppCompatActivity {
ListView listView;
ArrayList<Student> arrayList;
StudentAdapter studentAdapter;
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//khoi tao doi tuong DBHelper
db = new DBHelper(MainActivity.this);
//mapping listview
listView = (ListView)findViewById(R.id.list);
//load danh sach sinh vien
arrayList = db.getAllStudent();
studentAdapter = new StudentAdapter(MainActivity.this,
R.layout.layout_row,
arrayList);
listView.setAdapter(studentAdapter);
//dang ky menu COntext cho listView
registerForContextMenu(listView);
}
//them menu insert student
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
//Xu ly menu insert student
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.mnu_add){
final Student student = new Student();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Insert student");
builder.setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.layout_input,null);
final EditText edtName = (EditText)view.findViewById(R.id.edtName);
final EditText edtAddress = (EditText)view.findViewById(R.id.edtAddress);
final RadioGroup rdg = (RadioGroup)view.findViewById(R.id.rdgGender);
final ImageView img = (ImageView)view.findViewById(R.id.image);
builder.setView(view);
rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId){
case R.id.rdbMale:
student.setGender(1);
img.setImageResource(R.drawable.male);
break;
case R.id.rdbFemale:
student.setGender(0);
img.setImageResource(R.drawable.female);
break;
}
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
student.setName(edtName.getText().toString());
student.setAddress(edtAddress.getText().toString());
db.insertStudent(student);
arrayList.add(student);
studentAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
return super.onOptionsItemSelected(item);
}
//them menu Update va delete
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu,menu);
}
//xu ly menu Update va delete
@Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()){
case R.id.mnu_update:
final Student student = arrayList.get(info.position);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Update student");
builder.setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.layout_input,null);
final EditText edtName = (EditText)view.findViewById(R.id.edtName);
final EditText edtAddress = (EditText)view.findViewById(R.id.edtAddress);
final RadioGroup rdg = (RadioGroup)view.findViewById(R.id.rdgGender);
final RadioButton rdbMale = (RadioButton)view.findViewById(R.id.rdbMale);
final RadioButton rdbFemale = (RadioButton)view.findViewById(R.id.rdbFemale);
final ImageView img = (ImageView)view.findViewById(R.id.image);
edtName.setText(student.getName());
edtAddress.setText(student.getAddress());
if(student.getGender() == 1){
rdbMale.setChecked(true);
img.setImageResource(R.drawable.male);
}else {
rdbFemale.setChecked(true);
img.setImageResource(R.drawable.female);
}
builder.setView(view);
rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId){
case R.id.rdbMale:
student.setGender(1);
img.setImageResource(R.drawable.male);
break;
case R.id.rdbFemale:
student.setGender(0);
img.setImageResource(R.drawable.female);
break;
}
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
student.setName(edtName.getText().toString());
student.setAddress(edtAddress.getText().toString());
db.updateStudent(student);
arrayList.set(info.position,student);
studentAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
break;
case R.id.mnu_delete:
final Student student1 = arrayList.get(info.position);
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setTitle("Delete student");
builder1.setCancelable(false);
builder1.setMessage("Are you sure delete \" " + student1.getName() + "\"" );
builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
db.deleteStudent(student1);
arrayList.remove(info.position);
studentAdapter.notifyDataSetChanged();
}
});
builder1.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog1 = builder1.create();
alertDialog1.show();
break;
}
return super.onContextItemSelected(item);
}
}
|
Chúc các bạn học tốt!
Trung tâm Tin học ĐH Khoa học Tự nhiên