Empty array when using getListView().getCheckedItemIds()
<p>I am working on a sample app to learn android. It is supposed to display a multiple select list and when the user clicks on the button it should display the items selected in a TextView at the top. It seems that when I try to retrieve the selected items from the ListView an empty array is always being returned. Can anyone explain to me why this is the case and what I am missing to make it work correctly? (FYI the code is a modified program from Beginning Android 4 by Grant Allen)</p>

<p>Here is the layout xml file:</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;TextView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" /&gt;
    &lt;Button
        android:id="@+id/getSelections"
        android:text="Show Selections"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="listSelected" /&gt;
    &lt;ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:choiceMode="multipleChoice" /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>And here is the associated Java:</p>

<pre><code>package com.commonsware.android.checklist;

import android.os.Bundle;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.*;

public class ChecklistDemo extends ListActivity {
    private static final String[] items={"lorem", "ipsum", "dolor",
                    "sit", "amet",
                    "consectetuer", "adipiscing", "elit", "morbi", "vel",
                    "ligula", "vitae", "arcu", "aliquet", "mollis",
                    "etiam", "vel", "erat", "placerat", "ante",
                    "porttitor", "sodales", "pellentesque", "augue", "purus"};

    private TextView selection;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        setListAdapter(new ArrayAdapter&lt;String&gt;(this,
                                                android.R.layout.simple_list_item_multiple_choice,
                                                items));
        selection = (TextView)findViewById(R.id.selection);

    }

    public void listSelected(View view) {
        long[] chosenOnes = getListView().getCheckedItemIds();
        selection.setText("Items selected: ");
        selection.setText(Integer.toString(chosenOnes.length));
        for (long x:chosenOnes) {
            int i = Long.valueOf(x).intValue();
            selection.setText(selection.getText() + ", " + items[i]);           
        }

    }

}
</code></pre>
