Last Updated: July 16, 2018
· wannabegeekboy

Handle multiple click listeners in a listView

Being a android developer, we often face a common problem of handling multiple events in the list view (from a listview's adapter) and update UI elements which are defined in Activity file. For Instance, on click of list item row we might need to update some UI elements which is in the Activity.

Often we handle these events in the adapter by creating a event listener for the views, but if we want the update UI elements which are defined in Activity, then we might need to achieve this using interfaces - delegate design patter or other solution is pass the id of the views that needs to be updated to the adapter, but those might not be elegant solutions.

So simple solution is ,

defines the on click listener in the activity and pass it the activity and set this click listener to the views, so view event is triggered, we get a call to the onClick which is defined in the Activity.

Code snippet to achieve this is :

Constructor of the ListViewAdapter is

public ListAdapter(ArrayList<String> people, OnClickListener clickListener)
this.list = people;
this.clickListener = clickListener;

Define the getView as below,

public View getView(int position, View view, ViewGroup parent)
ViewHolder holder;
convertView = view;
if (convertView == null)
holder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
holder.nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
holder.callButton = (Button) convertView.findViewById(R.id.callButton);
holder.msgButton = (Button) convertView.findViewById(R.id.msgButton);
} else
holder = (ViewHolder) convertView.getTag();


setTagsToViews(holder.callButton, position);
setTagsToViews(holder.msgButton, position);

return convertView;

* Sets the onClickListener on the view
* @param view
private void setClickListeners(View view)

Here the listView layout contains the two buttons and a lable.

and in the Activity implement the setOnClickListener and my onClick function is

public void onClick(View v) {

int position = (Integer) v.getTag(R.id.key_position);
System.out.println("Postion" + position);

if (v.getId() == R.id.callButton) {
System.out.println("Call Button");

} else if (v.getId() == R.id.msgButton) {
System.out.println("Message Button");


In the above code snippet I have handled click events of two buttons and also we can update any UI elements which are defined in the activity inside the onClick method. Soon I ll upload the entire code in git

6 Responses
Add your response


How do we get holder.nameLabel.setText(list.get(position));? What is list?

How did you implement setTagsToViews()?

What does R.id.key_position represent and how can I create it?

over 1 year ago ·

The code is realy hard to read((

over 1 year ago ·

We have been looking here for if we can get robux. If you also want to know how to get free robux here is the website on which you all can rely to get the robux online for free.

over 1 year ago ·

Dude, I couldn't really understand this and I searched a bit more and found out this post from 2013 and it's obvious you plagiarized. You didn't even add any value in it, didn't even bother to indent the code snippets properly. Not cool man.

To help others who had questions, here is the link for the original post, which is thorough and really helpful.

11 months ago ·

You really should make use of Gist https://gist.github.com/
The way you post your code is horrible to read. You can even create a gist from Android Studio. Right Click over a Class and go to the last item "Create a Gist.

8 months ago ·

You can use setTag() and getTag() method with key and value concept.
holder.btnplus.setTag(R.integer.btnplusview, convertView);
plus.setTag(R.integer.btnpluspos, position);

Here, first parameter is key and second is value.

You can use below to get the tag
View tempview = (View) holder.btnplus.getTag(R.integer.btnplusview);
Integer pos = (Integer) holder.btn

Full and Nice example reference is available at: https://demonuts.com/listview-button/

You can use this method for multiple views like button, textview, checkbox etc. in the same row item of the listview.

4 months ago ·