2012-12-28 48 views
0

我正在爲選項卡組使用活動組。活動組中的Contextmenu

其中一個子選項卡組中的Activity活動包含一個Contextmenu。但是當我點擊菜單項時,在所選的上下文菜單項下編​​寫的代碼沒有運行。

the code which i was written in tabgroup Activity is given below 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    // TODO Auto-generated method stub 
    super.onCreateContextMenu(menu, v, menuInfo); 
} 
@Override 
public boolean onContextItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    return getLocalActivityManager().getCurrentActivity().onContextItemSelected(item); 
} 

,我寫在孩子Achtivity的代碼如下

public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) 
{ 
    super.onCreateContextMenu(menu, v, menuInfo); 
    if(v.getId()==R.id.appointment_listview1) 
    { 
     AdapterView.AdapterContextMenuInfo info = 
       (AdapterView.AdapterContextMenuInfo)menuInfo; 


     appointmnt_row item = (appointmnt_row)appListView.getItemAtPosition(info.position); 
     menu.setHeaderTitle(dateString); 
     menu.add(0, item.getId(), 0, "Delete"); 
    } 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 

    if(item.getTitle()=="Delete") 
    {Log.e("delete",""+item1.getId()); 
      } return true;} 

請給幫助我

+0

顯示onContextItemSelected方法 – Yahor10

+0

感謝您的互動。我認爲主要的問題是我使用的活動組,所以在兒童活動中實施上下文菜單是問題 – Vikky

+0

你有沒有嘗試? onContextItemSelected中的getContextItemSelected(item)中的getLocalActivityManager()。getCurrentActivity()。看到這個問題http://stackoverflow.com/questions/3157406/android-activitygroup-menu-problem – Yahor10

回答

0

呼叫

View view = getLocalActivityManager().startActivity("YourActivity", new 
              Intent(this,YourActivity.class) 
              .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
              .getDecorView(); 

      ListView list = (ListView) view.findViewById(android.R.id.list); // find your view,for example list. 
      list.setOnCreateContextMenuListener(this); 


@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    Log.v(null, "ITEM CREATED"); 
    menu.add(Menu.NONE, 0, Menu.NONE, "1 item"); 
    getLocalActivityManager().getCurrentActivity().onCreateContextMenu(menu, v, menuInfo); 
} 
@Override 
public boolean onContextItemSelected(MenuItem item) { 
    Log.v(null, "ITEM SELECTED"); 
    return getLocalActivityManager().getCurrentActivity().onContextItemSelected(item); 
} 

其作品對我來說

+0

好的代碼感謝您的幫助....在哪裏添加代碼? – Vikky

+0

在我的孩子活動我不能調用函數getLocalActivityManager() – Vikky

+0

這個代碼應該被添加到activityGroup類。只需調用findViewById並在兒童活動中設置聽衆 – Yahor10