Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:43:41+00:00 2026-05-23T16:43:41+00:00

I’m quite new to Android and I have been reading the Professional Android 2

  • 0

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 after 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:43:41+00:00Added an answer on May 23, 2026 at 4:43 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.