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.

RandomEngy
  • 14,931
  • 5
  • 70
  • 113

5 Answers5

7

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.

Mauricio
  • 133
  • 1
  • 5
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
3

set checkbox property focusable = false;

and run project again..

Crazy Coder
  • 508
  • 1
  • 4
  • 12
  • I met this problem, too. And fix it by adding focusable=false. But I don't understand why. Could you explain in details? – alexhilton Apr 14 '13 at 13:25
1

Found at this place: http://www.anddev.org/view-layout-resource-problems-f27/custom-list-view-row-item-and-context-menu-t52431.html

Setting the checkbox to not be focusable fixes the problem.

Not sure if it would cause issues when navigating the UI with something else than a touchscreen (with a wheel or arrow keys), but it fixed my problem (my layout was a bit more complicated than just a TextView and a Checkbox...)

Matthieu
  • 16,103
  • 10
  • 59
  • 86
0

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
  • 6,095
  • 10
  • 45
  • 54
  • 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 May 23 '10 at 05:10
0

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;
            }
        });
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202