how to use onClickListener method of Button on a ListView

15,979

Solution 1

In your Custom Adapater class's getView()

buttonDelete.setOnClickListener(new OnClickListener()
{
  @Override
  public void onClick(View v)
   {
     //  Use position parameter of your getView() in this method it will current position of Clicked row button
    // code for current Row deleted...              
   }
});

Solution 2

First, that will be very easy for you to handle the click on this item. You just have to add a listener with: myButton.setOnClickListener(mBuyButtonClickListener). This action is usually done on the getView(...) implementation of your ListView.

Your listener can know what is the item position of the button by using myListView.getPositionForView(myButton). Look at this sample of the listener :

private OnClickListener mBuyButtonClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        final int position = getListView().getPositionForView(v);
        if (position != ListView.INVALID_POSITION) {
            //DO THE STUFF YOU WANT TO DO WITH THE position
        }
    }
};

When this is done, you should have problems to handle the click on the item itself and some other touch event propagation.

So, you should read this article that fully describes the use of multiple touchable areas inside a ListView. It will be very helpful.

Solution 3

Use this inside the getView method of your adapter class.

holder.button.setOnClickListener(new OnClickListener()
{
  @Override
  public void onClick(View v)
   {

   }
});

Solution 4

I looked everywhere and non of the answers helped me until one did.

What i did was create a HolderView and wrapped up all my textviews and other widgets in the getView mathod of the adpater:

put this class in:

    static class ViewHolder
{
    TextView text1;
    TextView text2;
    TextView text3;
}

and i also decalared the same ones normally as they are seperate now:

TextView text1;
TextView text2;
TextView text3;

then declare all the views and widgets after inflating the layout in getView:

holder = new ViewHolder(); 
holder.text1 = (TextView) vi.findViewById(R.id.textView2single);
holder.text2 = (TextView) vi.findViewById(R.id.textView3single);
holder.text3 = (TextView) vi.findViewById(R.id.textView4single);

inside the onClick method, i used:

View parentView = (View) v.getParent();

and then declared all my widgets again using this parentview:

text1 = (TextView) parentView.findViewById(R.id.textView2single);
text2 = (TextView) parentView.findViewById(R.id.textView3single);
text3 = (TextView) parentView.findViewById(R.id.textView4single);

In doing this im forcing my onClick to get the parent position of the list view item, therefore all widgets inside that parent can be called easily, then you can do what you like with it. This is the only way i have found that works.

Share:
15,979
detno29
Author by

detno29

Updated on June 05, 2022

Comments

  • detno29
    detno29 almost 2 years

    I have a custom ListView which contains a Button. The function of this button is Delete button. Whenever user click to this button, the current Row will be deleted.

    • How can I do it?
    • How can I set onClickListener for this button?
    • How can I catch the row Id which this button is on?

    Thanks in advance.