Question

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);
                        }
                    });
            }
        });
Was it helpful?

Solution

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.

OTHER TIPS

Do it in your adapater, in the getview method. Set an onclick listner for every text view.

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    List<SectionObj> article = (List<SectionObj>) children.get(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.listchildlayout_galleryview, null);
    }       
     Gallery gallery = (Gallery) convertView.findViewById(R.id.customviewgallery);
     gallery.setAdapter(new AddImgAdp(context, article, groupPosition ));
     gallery.setOnItemClickListener(mOnGalleryClick);

    return convertView;
}

private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {


    }       
};

The above is for a gallery view. Do the same thing except do it for a textview

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top