mb0ura
Last Updated: February 25, 2016
·
19.54K
· 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,

@Override
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);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.nameTextView.setText(list.get(position));

setClickListeners(holder.callButton);
setClickListeners(holder.msgButton);

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

return convertView;
}

/**
* Sets the onClickListener on the view
*
* @param view
*/
private void setClickListeners(View view)
{
view.setOnClickListener(clickListener);
}

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

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

@Override
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

2 Responses
Add your response

17284
None

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

The code is realy hard to read((

over 1 year ago ·