Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple To-Do List example doesn't work on Android
    primarykey
    data
    text
    <p>I'm quite new to Android and I have been reading the <em>Professional Android 2 Application Development</em> 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):</p> <hr> <p>(Empty pace)</p> <hr> <p>Second task</p> <hr> <p>(Empty space)</p> <hr> <p>First task</p> <hr> <p>The code as it is written in the book and as it appears on the web page <a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-Android-2-Application-Development.productCd-0470565527,descCd-DOWNLOAD.html" rel="nofollow noreferrer">here</a> is the same, and there are not confirmed errors in the Errata section:</p> <pre><code>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 &lt;String&gt; todoItems = new ArrayList &lt;String&gt;(); //Create the array adapter to bind the array to the listview final ArrayAdapter &lt;String&gt; aa; aa = new ArrayAdapter &lt;String&gt; (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; } }); } } </code></pre> <p>My main.xml is like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;EditText android:id="@+id/myEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="New To Do Item" /&gt; &lt;ListView android:id="@+id/myListView" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>I know the bug comes somewhere from this line:</p> <pre><code>myEditText.setText(""); </code></pre> <p>If I comment out that line, then the program add two times what I have written:</p> <pre><code> Second Task Second Task First Task First Task </code></pre> <p>If I add something inside the quotes...</p> <pre><code>myEditText.setText("Error"); </code></pre> <p>It then prints it too:</p> <pre><code>Error Second task Error First Task </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload