Set item click event on setOnItemClickListener of listview Android

Swarna picture Swarna · May 12, 2011 · Viewed 57.8k times · Source

I have three textviews in a row of a listview using custom adapter and on click of the row, i want to perform the click event of the selected text view.

Below is my sample code for the click event. Here on the first click, the listeners are set and only on the second click the actual click event happens, I want to find this on the first click itself. Is it possible?

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view,      int pos, long id) {

                    final Order orderBooking = (Order)adapter.getItemAtPosition(pos);

                    // SKU listener
                    final TextView tvSkuId = (TextView) view.findViewById(orderBooking.getSelectedSkuId());
                    tvSkuId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onSkuClickListener(view, orderBooking);
                        }
                    });

                    // SO listener
                    final TextView tvSoId = (TextView) view.findViewById(orderBooking.getSelectedSoId());
                    tvSoId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onSoClickListener(view, orderBooking);
                        }
                    });

                    // OR listener
                    final TextView txtOrId = (TextView) view.findViewById(orderBooking.getSelectedOrId());
                    //onOrClickListener(view, orderBooking);
                    txtOrId.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onOrClickListener(view, orderBooking);
                        }
                    });
            }
        });

Answer

Egor picture Egor · May 12, 2011

Why not just set those clickListeners in time of initialization? You should make it in your Adapter, when you bind your data to the views. Your code is wrong, cause it will initialize your views every time the user clicks a list's row.