Mobile TV Elite Software

Posts Tagged ‘TO-DO LIST EXAMPLE’

One bug of the Android 2.x book: Professional Android 2 Application Development

The chap2 in the Android 2.x book “Professional Android 2 Application Development” gives a “TO-DO LIST EXAMPLE”, which will create a new to-do list Android application from scratch. There are 9 steps to create the to-do list application with using native Android View controls and the bug occurs on the final step:

“The final step to make this to-do list functional is to let users add new to-do items. Add an onKeyListener to the EditText that listens for a ‘‘D-pad center button’’ click before adding the contents of the EditText to the to-do list array and notifying the ArrayAdapter of the change. Then clear the EditText to prepare for another item.”

The added codes are bold:

myEditText.setOnKeyListener(new OnKeyListener() {
 public boolean onKey(View v, int keyCode, KeyEvent event) {
  if (event.getAction() == KeyEvent.ACTION_DOWN)
   if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
    todoItems.add(0, myEditText.getText().toString());
    aa.notifyDataSetChanged();
    myEditText.setText(“”);
    return true;

If you can’t run this to-do list application as the Android project, maybe the bug occurred on the below line:

myEditText.setOnKeyListener(new OnKeyListener() {

You should modify this line to:

myEditText.setOnKeyListener(new View.OnKeyListener() {

then the problem maybe resolved.

5 comments - What do you think?  Posted by CuteAndroid - March 22, 2010 at 12:00 pm

Categories: Android, Android book   Tags: , , ,