Android:如何使用ContextMenu制作可点击的ListView?

时间:2013-04-16 22:00:16

标签: java android listview contextmenu

我有一个可点击的ListView,并尝试为项目制作一个上下文菜单,但是如果我第一次激活onListItemClick方法而长时间点击它。下一次它的工作原理。 如果我使用openContextMenu激活上下文菜单,则会抛出StackOverflowError

我解决了我的问题!请参阅本文末尾。

我的活动:

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.search);

    getWords(getIntent());      
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id); 
        String lemma = (item.get(position));

        Intent intent = new Intent(this, EntryActivity.class)
            .putExtra(CL.DOC, doc)
            .putExtra(CL.ENTRY, lemma);
    startActivity(intent);
}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Eintrag");
    getMenuInflater().inflate(R.menu.context, menu);
}

public boolean onContextItemSelected(MenuItem item) {
    //TODO: switch
    return super.onContextItemSelected(item);
}

private void getWords(Intent intent) {
    doc = (CLDocument)intent.getSerializableExtra(CL.DOC);
    item = doc.getEntries();

    ArrayAdapter<String> fileList = 
        new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);   

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

我的布局(search.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edit_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:singleLine="true" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/nowords"
        />

</LinearLayout>

编辑:如果我激活openContextMenu

,这是logcat
04-17 11:31:06.103: D/dalvikvm(279): GC_FOR_MALLOC freed 5691 objects / 348744 bytes in 65ms
04-17 11:31:08.553: I/dalvikvm(279): threadid=1: stack overflow on call to Landroid/widget/AbsListView;.showContextMenuForChild:ZL
04-17 11:31:08.553: I/dalvikvm(279):   method requires 36+20+24=80 bytes, fp is 0x42089340 (64 left)
04-17 11:31:08.564: I/dalvikvm(279):   expanding stack end (0x42089300 to 0x42089000)
04-17 11:31:08.564: I/dalvikvm(279): Shrank stack (to 0x42089300, curFrame is 0x4208beb8)
04-17 11:31:08.573: D/AndroidRuntime(279): Shutting down VM
04-17 11:31:08.573: W/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-17 11:31:08.703: D/dalvikvm(279): GC_FOR_MALLOC freed 7370 objects / 493888 bytes in 50ms
04-17 11:31:08.733: E/AndroidRuntime(279): FATAL EXCEPTION: main
04-17 11:31:08.733: E/AndroidRuntime(279): java.lang.StackOverflowError
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1779)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.view.View.showContextMenu(View.java:2444)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.app.Activity.openContextMenu(Activity.java:2390)
04-17 11:31:08.733: E/AndroidRuntime(279):  at de.solich.cldic.SearchActivity$1.onItemLongClick(SearchActivity.java:99)
04-17 11:31:08.733: E/AndroidRuntime(279):  at android.widget.AbsListView.showC
04-17 11:31:08.823: D/dalvikvm(279): GC_FOR_MALLOC freed 3648 objects / 486736 bytes in 50ms

我更改了方法getWordsonCreateContextMenu,现在它可以正常运行:

private void getWords(Intent intent) {
    doc = (CLDocument)intent.getSerializableExtra(CL.DOC);
    item = doc.getEntries();

    ArrayAdapter<String> fileList = 
            new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);   

    registerForContextMenu(getListView());

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


}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info = 
            (AdapterView.AdapterContextMenuInfo)menuInfo;
    menu.setHeaderTitle(item.get(info.position));
    getMenuInflater().inflate(R.menu.context, menu);

}

感谢所有试图帮助我的人!

2 个答案:

答案 0 :(得分:1)

您必须使用registerForContextMenu作为活动ListView回调中的参数来致电onCreate()

答案 1 :(得分:0)

此处:getListView().setOnItemLongClickListener(...)长时间点击后您注册了上下文菜单(registerForContextMenu(view)),因此它只会在下次使用。但是,如果您希望上下文菜单从头开始工作,则应该更早地调用它,例如在registerForContextMenu(getListView())方法之前调用它。