在创建上下文菜单时抓住ListActivity的项目位置

时间:2011-01-12 23:37:29

标签: android listactivity

我有一个带有onClickListener的ListActivity,它成功捕获了项目的位置索引,然后我用它来获取Document对象的FeedList对象。我还希望能够在打开上下文菜单时获取此位置。我怎么做?代码片段: 我成功地在ListItemClick

上工作
    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    SearchResult sr = searchResults.get(position); // this is how I grab my document
    Toast.makeText(this, sr.toString(), Toast.LENGTH_SHORT).show();
}

有没有办法通过这些方法获取这个整数?

public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {

OR

public boolean onContextItemSelected(MenuItem item) {

谢谢你们!

1 个答案:

答案 0 :(得分:3)

这样的事情:

int lastPosition = -1;


public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info = 
        (AdapterView.AdapterContextMenuInfo) menuInfo;
    lastPosition = info.position;
}

public boolean onContextItemSelected(MenuItem item) {
    doStuff(lastPosition);
}