开发者

Simple To-Do List example doesn't work on Android

I'm quite new to Android and I have been reading the Professional Android 2 Application Development book from Wrox Press. One of the first tutorials is a simple To-Do List that just adds items one aft开发者_如何学运维er the other (it does not save the information or anything like that, that is taught later). The problem I'm having is it does not only prints what I write, it also adds an empty item, like this (sorry, I don't have over 10 reputation, so I can't post an image. Hope you get the idea):


(Empty pace)


Second task


(Empty space)


First task


The code as it is written in the book and as it appears on the web page here is the same, and there are not confirmed errors in the Errata section:

package com.paad.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 Todo_ListActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Get references to UI widgets
    ListView myListView = (ListView) findViewById(R.id.myListView);
    final EditText myEditText = (EditText)findViewById(R.id.myEditText);

    //Creates the array list of to do items
    final ArrayList <String> todoItems = new ArrayList <String>();

    //Create the array adapter to bind the array to the listview
    final ArrayAdapter <String> aa;
    aa = new ArrayAdapter <String> (this, android.R.layout.simple_list_item_1, todoItems);

    //Bind the array adapter to the listview
    myListView.setAdapter(aa);

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

My main.xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        android:text="New To Do Item"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

I know the bug comes somewhere from this line:

myEditText.setText("");

If I comment out that line, then the program add two times what I have written:

   Second Task

   Second Task

   First Task

   First Task

If I add something inside the quotes...

myEditText.setText("Error");

It then prints it too:

Error

Second task

Error

First Task

I looked up in the Android API, but I can't find a solution. Can someone help me out? How could I substitute that for something that works?


You get 2 events called each time you press a key : a KEY_DOWN, followed by a KEY_UP event. In your code, you don't filter those, so it just fires for the 2. The first one has text in the editText, while the second gets triggered after the editText's text has been flushed.

You can add in your onKey method something like to following :

if (event.getAction()!=KeyEvent.ACTION_DOWN)
     return true;

This will aknowledge the key down event, but you won't do anything with it. Then, when the key up comes by, you'll use that one.


Actually the KEYCODE_DPAD_CENTER is directional Pad Center key. May also be synthesized from trackball motions. You need to put up this code but it is not smooth.

OnKeyListener listener = new OnKeyListener(){
        public boolean onKey (View v, int keyCode, KeyEvent event){
            if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER && event.getAction()!=KeyEvent.ACTION_DOWN){
                todoItems.add(0, myEditText.getText().toString());
                myEditText.findFocus();
                myEditText.setText("");
                aa.notifyDataSetChanged();
                return true;
            }
            else return false;
        }
    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜