2012-03-28 52 views
0

我有一個ListView,它允許用戶長按一個項目來獲取上下文菜單。我遇到的問題是確定他們長期按下哪個ListItem。我有3列,(ID,文字,評論)。點擊時我需要檢索ID值。檢測哪個選擇的項目(在ListView多列中)催生了ContextMenu(Android)

我試着這樣做:

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    if (item.getTitle() == "Delete") { 
    View view = getWindow().getDecorView().findViewById(android.R.id.content); 
    //The rowId receive the ID clicked from the listview 
    rowId = ((TextView)view.findViewById(R.id.ID)).getText().toString(); 
    showDialog(0); 
    } else return false; 
    return true; 
} 

,但我總是cacth從列表視圖中的第一個項目的ID。如果我點擊listview上的第二個項目,我只收到列表中的第一個ID。

請任何幫助。

提前致謝。

回答

1

使用下面的代碼來獲得所選擇的行索引 -

public boolean onContextItemSelected(MenuItem item) { 
      try { 
       AdapterContextMenuInfo ctxMenuInfo; 
       try { 
        ctxMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); 
       } catch (ClassCastException e) { 
        return false; 
       } 

       int selectedPostion = ctxMenuInfo.position; 
} 
1

試試這個,如果你想提取從所選觀看本身你的信息。

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
View v = info.targetView; 
rowId = ((TextView)v.findViewById(R.id.ID)).getText().toString(); 
相關問題