Last Updated: January 24, 2018
· wannabegeekboy
380123 2865223158521 1527694351 n

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

Say Thanks

4 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 ·
 q0pluqv normal

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.

8 months 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.

2 months ago ·