views:

298

answers:

3

I've got a ListActivity and ListView and I've bound some data to it. The data shows up fine, and I've also registered a context menu for the view. When I display the list items as just a simple TextView, it works fine:

<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nametext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

However when I try something a bit more complex, like show the name and a CheckBox, the menu never shows up:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView android:id="@+id/nametext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/namecheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

Can long-presses work on more complex elements? I'm building on 2.1.

(edit)

Registering with this on the ListActivity:

registerForContextMenu(getListView());

The code I posted is the item template for the list.

A: 

Context menu's can only be registered to subclasses of View. I don't know how you registered the LinearLayout with a context menu, did you package it in some type of View? if so, you should post that code.

Anyways why not just register the TextView of each list item? Who would long press a checkbox...

Nathan
Thanks. I added the line where I register the context menu. Should I be registering each TextView in the list instead? Would I need to re-register when items are added?
RandomEngy
`LinearLayout` is a `View`.
CommonsWare
+3  A: 

Your CheckBox may be interfering with matters. Consider using a CheckedTextView instead of a LinearLayout, CheckBox, and TextView combination, since CheckedTextView is what Android expects for a CHOICE_MODE_MULTIPLE list.

Check out $ANDROID_HOME/platforms/$VERSION/data/res/layout/simple_list_item_multiple_choice.xml, where $ANDROID_HOME is wherever you installed the SDK and $VERSION is some Android version (e.g., android-2.1). This resource is the standard resource you should use for CHOICE_MODE_MULTIPLE lists. Feel free to copy it into your project and adjust the styling of the CheckedTextView as needed.

Here is a sample project showing a context menu used on a CHOICE_MODE_MULTIPLE ListView with CheckedTextViews as its rows.

CommonsWare
Thanks, that worked perfectly!
RandomEngy
Well maybe not quite. View bindings don't work anymore. It appears that when the CheckedTextView items are part of a list, you can't check them manually.
RandomEngy
Use `setItemChecked()` on your `ListView`.
CommonsWare
A: 

This should from a regular ListView as well. But if you're starting from scratch on a new list i would consider using the CheckedTextView

        checkBox.setOnLongClickListener(new View.OnLongClickListener() {

            public boolean onLongClick(View v) {
                // return false to let list's context menu show
                return false;
            }
        });
dmitri926