TomaszRykala
TomaszRykala

Reputation: 947

grab ListActivity's item position when creating a Context Menu

I've got a ListActivity with a onClickListener which succesfully captures the item's position index which I then use to grab objects of my feeding ArrayList of Document objects. I would also like to be able to grab this position int when opening a Context Menu. How do I do that? Code snippets: My successfuly working onListItemClick

    @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();
}

Is there a way to grab this integer by these methods?

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

OR

public boolean onContextItemSelected(MenuItem item) {

Thanks guys!

Upvotes: 0

Views: 729

Answers (1)

peter3
peter3

Reputation: 1074

Something like this:

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);
}

Upvotes: 3

Related Questions