简体   繁体   中英

adding itemClickListener on items e.g.(TextViews, Images, Buttons) within in a customized ListView

Can we add itemClickLIstener on items eg(TextViews, Images, Buttons) within in a customized ListView? if YES? Then where we will write the listener code

sure.. you can add it by two ways

1). by defining the functions in xml file and define those functions in getview or bindview of your custom adapter class.

2). or by defining the onclick listeners in bindview or getview of custom adapter class.

    @Override
public View getView(int position, View view, ViewGroup parent) {

    if (view == null) {
        Button btn = (Button) view.findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
    }

    return view;

}

You can add onClickListeners on Items that you defined in your customized ListView. Refer the below code

holder.txtIncident.setOnClickListener

public View getView(final int position, View convertView,
            ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.headline_item, null);
            holder = new ViewHolder();

            holder.txtIncident = (TextView) convertView
                    .findViewById(R.id.txtIncident);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtIncident.setText(String.valueOf(itemList.get(position)
                .getIncident()));

        holder.txtIncident.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        return convertView;
    }

Yes, you can create listeners for components in each list item. You can do this by creating a custom list adapter and set the listener in its getView method.

    public class MyAdapter extends ArrayAdapter<String> {

        private int resourceId;
        private String[] objects;

        public MyAdapter(Context context,  int resourceId, String[] objects) {
           super(context, resourceId,objects);
           this.resourceId = resourceId;
           this.objects = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(resourceId, null);
            }

            //Make sure that you use convertView.findViewById and not just findViewById!
            TextView textView1 = convertView.findViewById(R.id.yourTextViewsId);
            textView1.setText(objects[position]);
            textView1.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View view) {
                    //do something here
                }
            });

            return convertView;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM