Now you can Subscribe using RSS

Submit your Email

Tuesday

OnClickListener on custom list view's button

Vishal Shrestha
This post assumes you have knowledge of making custom lists using your own adapters.
If you are not sure how to do that, you can learn it here
Now what we need to do is to set onClickListener to a button inside the custom row of the list. Setting onLongClickListenter to the row is simple as we don't care about the elements inside the row, but if you want to set onClickListener to a button inside the row, you can do it the following way.

  • First refer to the button for which you want to set the listener in the getView() method of your Adapter.
  • Then Simply create the onClick() function outside the getView(), but inside the Adapter.
Here's a sample code from an app i was building-


@Override

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.customlistview, parent, false);
} else {
row = convertView;
}
favt = (ToggleButton) row.findViewById(R.id.toggleButton1);
favt.setOnClickListener(this);
TextView chapter = (TextView) row.findViewById(R.id.chapter);
TextView content = (TextView) row.findViewById(R.id.content);
chapter.setText(chapterList.get(position));
content.setText(contentList.get(position));

return row;
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
// Your onClick handling code here
}

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha