2010-03-16 19 views
30

Tôi có chế độ xem danh sách chứa dữ liệu. Tôi thiết lập một menu ngữ cảnh cho listview sử dụng đoạn mã sau:Android: Cách tìm vị trí được nhấp từ menu ngữ cảnh

list.setOnCreateContextMenuListener 
(
    new View.OnCreateContextMenuListener() 
    { 
     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) 
     { 
     AdapterContextMenuInfo mi =(AdapterContextMenuInfo) menuInfo; 
     menu.add(0, 0, 0, "Delete item");     
    } 
    } 
); 

Tôi có override phương pháp sau đây để kiểm soát de ContextMenu menuitem chọn:

@Override 
public boolean onContextItemSelected(MenuItem item) 
{ 
    switch(item.getItemId()) 
    { 
    case 0: 
    ShowAlert("hello from delete item"); 
    break; 
    default: 
    return super.onContextItemSelected(item); 
    } 
    return true; 
} 

Trong phương pháp overrided này, làm thế nào có thể tôi tìm mục của chế độ xem danh sách đã được nhấp?

Xin cảm ơn trước. Trân trọng. Jose

Trả lời

75

Bạn có thể sử dụng ContextMenu.ContextMenuInfo.

Something như thế:

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    int index = info.position; 
} 

Bạn cũng có thể nhận được chính xác View mà menu đang được hiển thị:

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    int index = info.position; 
    View view = info.targetView; 
} 
+0

Cảm ơn rất nhiều. Trân trọng. jose – Sosi

+1

thông tin luôn luôn là rỗng trong trường hợp của tôi .. bất cứ ai có thể giúp đỡ? Tôi đang sử dụng GridView. –

+1

Cuối cùng tôi đã làm việc đó, tôi đã sử dụng GridView tùy chỉnh trong đó phương thức 'getContextMenuInfo()' không được triển khai. Tôi đã đăng [ở đây] (http://vinaybhargav.wordpress.com/2014/07/20/android-floating-context-menu-for-listviewgridview/) trong trường hợp nếu ai đó cần một mẫu. –

3
private static final int EDIT_ID = Menu.FIRST + 3; 
private static final int DELETE_ID = Menu.FIRST + 4; 
@Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenu.ContextMenuInfo menuInfo) { 
     menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit").setAlphabeticShortcut(
       'e'); 
     menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete") 
       .setAlphabeticShortcut('d'); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item 
       .getMenuInfo(); 
     switch (item.getItemId()) { 
     case EDIT_ID: 

      edit(info.id); 
      return (true); 
     case DELETE_ID: 

      delete(info.id); 
      return (true); 
     } 

     return (super.onOptionsItemSelected(item)); 
    } 
+0

Thanks a lot. Trân trọng. Jose – Sosi

+0

tôi đã thử điều này với recyclerview nhưng có vẻ như không hoạt động. bất kỳ cách giải quyết nào? –

0

OK, để giải quyết vấn đề thông tin vô **** sử dụng thành viên tĩnh và giá trị được đặt từ Vị trí của chủ sở hữu của bạn để lưu giá trị trong thành viên phương thức longclick ví dụ: -

public class CurrentPosition { 
    public static int Pos{ get; set; } 
} 

public bool OnLongClick(View v) 
{ 
    CurrentPosition.Pos = Position; 
    return false; 
} 

và sử dụng trong chọn mục ngữ cảnh của bạn:

public override bool OnContextItemSelected(IMenuItem item) 
{ 
    switch (item.ItemId) 
    { 
     case 0: 
      return true; 
     case 1: 
      Toast.MakeText(this,CurrentPosition.Pos.ToString(), ToastLength.Long).Show(); 
      return true; 
     case 2: 
      Toast.MakeText(this, "Save", ToastLength.Long).Show(); 
      return true; 
     } 
     return true; 
    } 
} 

C# mã

Các vấn đề liên quan