'How can I add onClickListener into TableLayout row buttons in android?

I have the following code:

tableLayout = (TableLayout) findViewById(R.id.tableLayout);




        for (int i = 0; i < coordinates.size(); i++) {
            View tableRow = LayoutInflater.from(this).inflate(R.layout.activity_helper, null, false);
            TextView display_no = (TextView) tableRow.findViewById(R.id.display_no);
            TextView display_coordinate = (TextView) tableRow.findViewById(R.id.display_coordinate);

            Button onMyWayButton = (Button) tableRow.findViewById(R.id.buttonOnMyWay);
            Button cancelButton = (Button) tableRow.findViewById(R.id.cancelButton);

            int finalI = i+1;
            cancelButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.e("Which element: ", finalI +".");
                }
            });

            onMyWayButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.e("Which element: ", finalI +".");
                }
            });



            display_no.setText("" + (i + 1));
            display_coordinate.setText(coordinates.get(i).getCoordinate());

            tableLayout.addView(tableRow);
        }

After it generated the rows, in the first row the buttons doesen't log anything. The rest of the buttons are working fine.

What did I miss?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source