You are on page 1of 5

B5: Time to coding. Ti th mc src/Example.

java v thay i ni dung file nh


sau:
M:
package at.exam;

import java.util.ArrayList;

import android.app.Activity;
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);

//To mng cha String ni dung cng vic v
gi
final ArrayList<String> arrayWork = new
ArrayList<String>();
//Adapter dng kt ni mng vi List View
final ArrayAdapter<String> arrayAdapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
arrayWork);

//Cc EditText vo ni dung cng vic c
ly v t 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 nhn s thm cng vic vo
ListView
final Button button = (Button)
findViewById(R.id.button);

//ListView cha danh sch cng vic
final ListView list = (ListView)
findViewById(R.id.list);
//Cn set Adapter cho list bit s ly ni
dung t mng arrayWork
list.setAdapter(arrayAdapter);

//nh ngha Listener x l s kin nhn vo
button
OnClickListener add = new OnClickListener() {
@Override
public void onClick(View v) {
//Nu 1 trong 3 Edit Text khng c ni
dung th hin ln thng bo
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();
}
//Ly ni dung cng vic v thi gian
ra t Edit Text v a vo 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);
}
}
Mnh ch thch y v on code cng kh d hiu. Tuy nhin cn lu 2
vn y.
- Khi to i tng ArrayAdapter: Cc bn thy i s truyn vo l (this,
android.R.layout.simple_list_item_1, arrayWork). This l i s ca lp Context
( y chnh l activity Example). Bn s gp Context trong rt nhiu khi to cc
lp v nn hiu Context c ngha g. Mnh xin a ra gii thch ca anh Gip
(thank mr giaplv):
Quote:
Context thuc android.content (android.content.Context).

L mt Interface (lp giao tip) cha hu ht thng tin v mi trng ng dng ca
android, c ngha l mi thao tc, tng tc vi h iu hnh iu phi qua lp
ny.
N l mt lp abstract (tru tng) cung cp cho nhng lp khc cc phng thc
tng tc vi h thng Android.
N cho php truy cp ti cc ngun ti nguyn (resources) c nh ngha v
cc lp khc. V d nh n c th khi to v chy cc activities, cc broadcast v
cc intents,... Chng ta coi nh Contex l mt lp mc ng dng (Application
level- lin quan ti h thng).

Tm li context gip chng ta d dng truy cp v tng tc ti cc ti nguyn ca
h thng, cc thng tin, cc dch v (services), cc thng s cu hnh, database,
wallpaper, danh b, cuc gi, kt ni, ch rung (vibrator),...

***s d hu ht cc lp c lin quan ti UI (layout, button, textview, imageview,
listview,...) u pi super ti Context v bn thn n m nhim vic truy cp
resource (R.id, R.layout,....). Nu chng ta khng tham chiu ti Context class th
ng nhin khng th dng ti cc resources m chng ta to ra.
Tip theo l android.R.layout.simple_list_item_1, i ny nh ngha cch th hin
item ( y l String) trong List View. Cc bn hy ghi nh android.R.* l cc ti
nguyn (resource) c sn ca Android cho php bn truy cp v s dng. Sau ny
khi hng dn to custom View cho List View mnh s cp li vn ny.
Cui cng arrayWork chnh l mng cn c bind ca adapter.

- AlertDialog l lp cho php a ra 1 hp thoi, thng dng a ra thng tin
hoc cnh bo n gin. Trong code mnh to 1 builder, to tiu (title) cho n,
a ra thng bo (message) v cui cng l to 1 positive button (nhng khng
nh ngha x l khi nhn nt ny, v vy nu bn nhn nt th dialog s ch n
gin thc hin vic ng li).

B6: Tin hnh chy th chng trnh. Run as -> Android Application. Enjoy
yourself

You might also like