Q:

Android - Make ImageView Visible When ListItem is selected

I have a listview and each item has a title, some info, and a couple ImageViews I'm using as edit/delete buttons.  I don't want to show these "buttons" unless the user selects the row.  I can make the "buttons" invisible using:
        DeleteButton.setVisibility(View.INVISIBLE);
        EditButton.setVisibility(View.INVISIBLE);

in my BindView.  
I can make the buttons visible in an onListItemClick:
    ImageView DeleteButton = (ImageView) v.findViewById(R.id.button_delete);
    ImageView EditButton = (ImageView) v.findViewById(R.id.button_edit);
    DeleteButton.setVisibility(View.VISIBLE);
    EditButton.setVisibility(View.VISIBLE);

What I can't do is make the "buttons" invisible when selecting another item or scrolling away.
The closest I found was to do a loop through the listitems in the current view and set them all to invisible before making the selected one visible:
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    \\loop through all the items and set them back to invisible
    for (int i=0;i<=l.getLastVisiblePosition();i++){
        View vChild = l.getChildAt(i);
        ImageView DeleteButton = (ImageView) vChild.findViewById(R.id.button_delete);
        ImageView EditButton = (ImageView) vChild.findViewById(R.id.button_edit);
        DeleteButton.setVisibility(View.INVISIBLE);
        EditButton.setVisibility(View.INVISIBLE);
    }

    \\set the selected one visible
    ImageView DeleteButton = (ImageView) v.findViewById(R.id.button_delete);
    ImageView EditButton = (ImageView) v.findViewById(R.id.button_edit);
    DeleteButton.setVisibility(View.VISIBLE);
    EditButton.setVisibility(View.VISIBLE);

}

As you can guess...this approach only works if you have a few items.
I thought about adding a field to the SQLite database my list is using to keep track of the button visibility (similar to what you do for checkboxes) but that seemed silly.  Please tell  me there's another way.

A:

Another way to do this would be to have an int field in your class that will remember the current position:
private int current = -1;

then in the onItemCLick() method use that field to hide/show your views:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
            // if it is the first click ignore this part
    if (current != -1) {
        View last = l.getChildAt(current); // the last one clicked
        last.findViewById(R.id.button1).setVisibility(View.GONE); // kill it
    }
    v.findViewById(R.id.button1).setVisibility(View.VISIBLE);
    current = position; // remember the new clicked position
}

If you want the views to be gone also when you scroll the list and those views aren't visible then in the bindView() method add the lines that hide the views:
//...
ImageView DeleteButton = (ImageView) view.findViewById(R.id.button_delete); // view is the view that you get as a parameter
ImageView EditButton = (ImageView) view.findViewById(R.id.button_edit);
DeleteButton.setVisibility(View.INVISIBLE);
EditButton.setVisibility(View.INVISIBLE);

//...

When the use scrolls the list all the views will have the Buttons visibility set to GONE and the onItemCLick() logic will work only for visible views.

