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.
Related Android Apps:
- Cute Android Apps for Users: Aldiko Book Reader
- Open Source Android Apps for Developers: StyloDownload (Android application for syncing Google Reader starred items to the media player)
- Open Source Android Apps for Developers: Livingwallpaper (Living wallpaper application for the Google Android platform)
- Open Source Android Apps for Developers: Azilink (usb tethering application for android)
- Open Source Android Apps for Developers: Diskusage (Diskusage application for android phone)
- Android books for developers:part one
- Android books for Users: Google on the Go
- Open Source Android Apps for Developers: FBReaderJ (An e-book reader for Google Android platform)
- Cute Android Apps for Users: How to Draw cartoons
- Android 2.x books for developers
Hey,
I was trying to work and the code gives me problems despite what you suggested. The application keeps crashing unexpectedly without even opening once, any idea what should be done. Here is the code I m trying:
package com.todolist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class todolist extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
// Inflate your view
setContentView(R.layout.main);
// Get references to UI widgets
ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText myEditText =
(EditText)findViewById(R.id.myListView);
// Create the array list of to do items
final ArrayList todoItems = new ArrayList();
// Create the array adapter to bind the array to the listview
final ArrayAdapter aa; aa = new
ArrayAdapter(this,
android.R.layout.simple_list_item_1,
todoItems);
// Bind the array adapter to the listview.
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new View.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;
} return false;
} });
}
[Reply]
CuteAndroid Reply:
June 25th, 2010 at 12:01 pm
Code seems ok, I can’t find any problem within it. You can debug it by using
Android logcat, sorry.
[Reply]
Hey varun,ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText myEditText =
(EditText)findViewById(R.id.myListView);
you are using the same resource id for both listview and edittext. this is your problem. use R.id.myEditText
[Reply]
Patel Ankit Reply:
March 16th, 2011 at 7:21 am
can’t apply same name for edit text and list view because all id store
R.id class file.
thanks
[Reply]
Awesome, thanks for this tip
[Reply]