views:

77

answers:

1

I am using a ListView with an ArrayAdapter that holds objects. When I select an item, I am capturing the position and index of the selected item. If I scroll down prior to selection, the position and index represent the location of the item in the list. Selecting that items takes me to another activity. When I use the back button to return to the list, it seems that the ListView gets a new position and index for the visible items.

As a result, I can't figure out how to reference the selected item during the restart() of the ListView Activity. I have tried to capture position and index, but as I've said, they change upon returning to the Activity.

Is my understanding of the ListView "redraw" correct? Does it renumber my items based on what's visible? -When in the life cycle is getView() called? Is there a way to force an update to the ListView so that my captured index still points to the same object?

Thanks, Jason

A: 

If you have the same elements in your ListView you will have the same position all the time, when you click an Item...

    @Override
    protected void onListItemClick(ListView l, View v, final int position, long id) {
        super.onListItemClick(l, v, position, id);
Toast.makeText(this, "This is the Item " + position + " of my listView",
                Toast.LENGTH_LONG).show();
        });

the same in you getView() function...

  public View getView(int position, View convertView, ViewGroup parent) {
    Toast.makeText(this, "This is the Item " + position + " of my listView",
                        Toast.LENGTH_LONG).show();          
Jorgesys
When I click the Item, I go to another Activity. When I hit the back button, the "visible" list is renumbered with the top Item starting at zero. The new position is not the same. It seems like an optimization behavior. When this happens I lose reference to the previously selected Item.I am changing an image in the ListView upon return. If I scroll down slightly and select, lets say Item 5. When I return, the icon in the 5th Item from the top Of the visible list changes. Which may have been 7th before.
tunneling
when you click the item for the first time in your ListView the item position must be the position starting from Zero! post the code of your implementation
Jorgesys
Activity A is a ListView. Activity B is a controller for the Item selected from Activity A. When returning from Activity B to Activity A... the "old" position does not equal the "new" position of the selected item. I have noticed that the "new" position of the Item is assigned based on the location from the top of what is being displayed. I'll post the code when I have access to it. Thanks.
tunneling