public class CustomViewGroup extends LinearLayout { public CheckBox cb; public TextView workContent; public TextView timeContent; public CustomViewGroup(Context context) { super(context);
//S dng LayoutInflater gn giao din trong list.xml cho class ny LayoutInflater li = (LayoutInflater) this.getContext()
//Ly v cc View qua Id cb = (CheckBox) findViewById(R.id.check_work); workContent = (TextView) findViewById(R.id.work_content); timeContent = (TextView) findViewById(R.id.time_content); } } on code trn gip ta nh ngha giao din ca custom ViewGroup mi da trn file list.xml. Mi ngi cng c th to giao din bng code, ko cn s dng XML nhng s phc tp hn v mnh cng ko gii thiu y.
B7: To 1 class Work cng trong at.exam th hin cng vic: M: package at.exam;
public class Work { private String workContent; private String timeContent; private boolean isChecked;
//Phng thc xc nh View m Adapter hin th, y chnh l CustomViewGroup //Bt buc phi Override khi k tha t ArrayAdapter @Override public View getView(int position, View convertView, ViewGroup parent) { View workView = convertView;
if (workView == null) { workView = new CustomViewGroup(getContext()); }
//Ly v i tng Work hin ti final Work work = array.get(position);
//Ly v ni dung cho TextView v CheckBox da vo i tng Work hin ti workContent.setText(work.getContent()); timeContent.setText(work.getTime()); checkWork.setChecked(work.isChecked()); } return workView; } } ListWorkAdapter s c s dng thay th cho ArrayAdapter c bind vi ListView. Thng thng ArrayAdapter ch cho hin th String bng TextView, nhng vi vic k tha v override phng thc getView, ta c th nh ngha li hin th cho cc thnh phn ca ListView.