Giờ tới Code. Tới thư mục src/Example.java và thay đổi nội dung file như sau:
package at.exam;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class Example extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Tao mang de chua String noi dung cong viec va gio
final ArrayList<String> arrayWork = new ArrayList<String>();
//Adapter dung de ket noi mang voi List View
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayWork);
//Cac EditText de vao noi dung cong viec duoc lay ve tu XML
final EditText workEnter = (EditText)findViewById(R.id.work_enter);
final EditText hourEdit = (EditText)findViewById(R.id.hour_edit);
final EditText minuteEdit = (EditText)findViewById(R.id.minute_edit);
//Button khi nhan se them cong viec vao ListView
final Button button = (Button)findViewById(R.id.button);
//ListView chua danh sach cong viec
final ListView list = (ListView)findViewById(R.id.list);
//Can set Adapter cho list de biet se lay noi dung tu mang arrayWork
list.setAdapter(arrayAdapter);
//Ðinh nghia Listener xu ly su kien nhan vào button
OnClickListener add = new OnClickListener() {
@Override
public void onClick(View v) {
//Neu 1 trong 3 Edit Text khong co noi dung thi hien len thong bao
if(workEnter.getText().toString().equals("") || hourEdit.getText().toString().equals("") || minuteEdit.getText().toString().equals("")) {
AlertDialog.Builder builder = new AlertDialog.Builder(Example.this);
builder.setTitle("Info missing");
builder.setMessage("Please enter all information of the work");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.show();
}
//Lay noi dung cong viec va thoi gian ra tu Edit Text va dua vao list
else {
String str = workEnter.getText().toString() + " - " + hourEdit.getText().toString() + ":" + minuteEdit.getText().toString();
arrayWork.add(0,str);
arrayAdapter.notifyDataSetChanged();
workEnter.setText("");
hourEdit.setText("");
minuteEdit.setText("");
}
}
};
//set Listener cho button
button.setOnClickListener(add);
}
}