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
Written by Gautam Basubramanian
Related protips
4 Responses
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?
The code is realy hard to read((
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.
You can use setTag() and getTag() method with key and value concept.
Example:
holder.btnplus.setTag(R.integer.btnplusview, convertView);
holder.btnplus.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.btnplus.getTag(R.integer.btnpluspos);
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.