开发者

ContextMenu will not appear on ListView when using SimpleCursorAdapter

开发者 https://www.devze.com 2023-04-12 04:05 出处:网络
I\'m new to android development and I work on the application for tablet running Android 3.1. The main activity is divided into more functional parts and one of them shows the list of some items, the

I'm new to android development and I work on the application for tablet running Android 3.1. The main activity is divided into more functional parts and one of them shows the list of some items, the long click on the item should open the context menu. I have a problem, because I can't make it work.

The main activity extends the MapActivity because it contains also the map... The ListView uses the SimpleCursorAdapter.

The list view is registered for context menu in the onCreate method of the main activity:

  alarmList = (ListView) findViewById (R.id.alarmList);
  registerForContextMenu(alarmList);

And the main activity overrides onCreateContextMenu and also onContextItemSelected methods, but this are never called when the item is clicked.


I wondered that while using the ArrayAdapter and simple string display of the item, the context menu of the 开发者_如何学Pythonlist works well and the list provides a nice selection of items when clicked (with some nice fade-out effect). But when I change the adapter to SimpleCursorAdapter, the list view does not react on clicking at all. To "simulate" the clicks I have added clickable attribute to ListView:

<ListView android:id="@+id/alarmList" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true"/>

And the single list item is formatted using its own layout, which starts like this: [alarm_list_item.xml:]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout" android:layout_width="match_parent" android:background="@color/alarm_list_bg"
    android:clickable="true" android:baselineAligned="true" android:layout_height="wrap_content" android:minHeight="65dp">
...

[alarm_list_bg:]

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false" android:drawable="@color/black_12"/>
<item android:state_pressed="true"  android:drawable="@color/grey_12" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="@color/blue_12" />
</selector>

I have tried to register some listeners directly on the ListView instance (OnItemLongClickListener, OnCreateContextMenuListener, OnItemClickListener), but none of them is called.

Could you please have an idea what could be wrong?

Kind regards Mirec


Try to start with something like

    alarmList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
                    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
                            menu.add(0, ENTRY_ADD, 1, "Add");
                            menu.add(0, ENTRY_REMOVE , 2,"Remove");
                    }}); 

If it doesn't work, remove all unnecessary attributes from XML, like android:clickable - go with minimal working example, then find out what was causing problem.


Try to add this attribute to the checkbox:

focusable="false"


mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            return false;
        }
    });

remember to return false at the end of the function 'onItemLongClick'.If you return ture,system would think that the function 'onItemLongClick' "eat" the long press event.


ListView does not handle long click until you set setClickable(true) method to true

listviewobject.setClickable(true)


A late answer, but it may help someone out. In my case the Context Menu was not showing because my list adapter had stable ids.

Removing the following block solved the issue:

@Override
public boolean hasStableIds() {
    return true;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号