2012-03-28 96 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(); 
相关问题