0

Sorry for my English, I use Google translator. There is ListView. There is an event handler AdapterView.OnItemClickListener (). Inside it is changing the layout of the ListItem and appointed handlers onClick imageviev. The following code passes the point.

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {

        ... Here we change the layout

        /**
        * Bind the event handlers for new control
        */
        // Get UI-control 
        ImageView photoCapture = (ImageView) newInnerView.findViewById(R.id.listview_element_report_row_imageview);
        // set handler
        photoCapture.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                mCurrentItemPosition = position; ///??????????

                ...doing something useful

             }
         });
     }
 });

The sequence of events like this - we click on the element and it changes its layout (expandable). To one of the child elements we bind handler. External handler has a formal parameter position, the value I use the internal handler (but later), because these two events separated in time. I can expand an first ListItem, then expand the second, third,... and then return to the first and click ImageView and cause internal event handler. Question - what value will have a variable position in View.OnClickListener() ? I see - position always have "right" value, why?

petrumo
  • 1,116
  • 9
  • 18
mamachiao
  • 11
  • 1
  • 1st question) the setOnItemClickListener it registers the listener on the list view. So, when the user clicks an item, this listener is called with information about the view, from the list, which got clicked. – petrumo Dec 16 '16 at 20:44
  • 2nd question) is about about final variables accessible in the method scope, it's a pure java question. Here is some post explaining in more details http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – petrumo Dec 16 '16 at 20:51
  • I read about the Closure. In other words, it's a kind of magic - value "position" obtained by calling the "external" function is preserved for each(!) of the clicked ListItem, and when call the internal handler can used them? – mamachiao Dec 16 '16 at 21:08
  • Thank you for your reply, but can you dispel my doubts? This is especially magical when you consider that ListView are reused ItemView (there assigned event handler) , and in fact they are less than the data source contains.Otherwise, one could assume that as long as there are instances object, some special variables (that are not members of a class) are stored in a special way. But as I said above - used items is less than the total number, and unique values and special variables ("position" in my case) may not be sufficient in the general case or they may be confused between an instance. – mamachiao Dec 17 '16 at 00:07
  • I am trying to explain in the simplest form, the imagelisteners are only created for those items that are clicked from the list and the image listener is linked to the specific item "position" value. There is no magic about the "position" parameter, as the variable is marked as final, it's value is available for the scope within that method including any anonymous classes or the listener scope. – petrumo Dec 17 '16 at 11:29

0 Answers0