how to use onClickListener method of Button on a ListView
Asked Answered
L

7

5

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.

Lykins answered 21/7, 2012 at 10:45 Comment(2)
setOnItemClickListener() on listview if u need only row idExaminee
See this ExampleBowers
V
7

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...              
   }
});
Venegas answered 21/7, 2012 at 10:48 Comment(1)
Which I earlier need to findById ?Thomson
H
4

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.

Hussar answered 22/7, 2012 at 0:14 Comment(0)
H
1

Use this inside the getView method of your adapter class.

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

   }
});
Heterogynous answered 21/7, 2012 at 11:11 Comment(0)
C
1

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.

Cavalcade answered 21/8, 2013 at 23:42 Comment(0)
V
0

Create a custom list view adapter, extend BaseAdapter and implement OnClickListener

public class ListAdapter extends BaseAdapter implements OnClickListener {}

override the getView method

@Override
public View getView(int
        position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = convertView;
    ViewHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();
        holder.b1= (Button) row.findViewById(R.id.b1);
        b1.setOnClickListener(this);
    else {

        holder = (ViewHolder) row.getTag();

    }
}

now override the onclick method

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
        case R.id.b1:
            //button b1 click event 
            break;
    }
}

public class ViewHolder {
    Button b1;
}
Vassili answered 25/7, 2014 at 17:21 Comment(0)
M
0

Create a new View type variable and assign it with currenView(Check if iit is null).Define variables(ImageButton taken here) and set viewId in the below mentioned way.

 public View getView(int position, View convertView, ViewGroup parent) {
        View itemview=convertView;
        if (itemview==null)
        {
            itemview=getLayoutInflater().inflate(R.layout.group_list,parent,false);

        }
        final group_contacts currentcontact=g_contact.get(position);
        ImageButton delete=(ImageButton) itemview.findViewById(R.id.imgbtn_delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Add your code here to run on tapping button
            }
        });
        return itemview;


    }
Mablemabry answered 6/4, 2015 at 5:35 Comment(0)
I
0

To use onClickListener() method on a Button of a ListView, you Must Apply onClickListener() on the Button in Adapter's getView Methode.

But the problem is, you will not be able to get the Value from the positionparameter of getView.

To get the positionof the Button you have Touched, you must Call getPositionForView()method on your ListView.

Lets discus with an Example For Better Understanding,

  • Consider you have a ListViewnamed ExampleListview

  • A Button on it named ExampleButtonand u want to use onClickListener() on it.

    Examplebutton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
            final int position= ExampleListview..getPositionForView(v);
       /*Where 'v' is actually the 'View' object Which you are clicking.*/ 
    
    /*you can now right your code her & determine the your desired button by
      using the position parameter */
    }
    
Incumbency answered 19/4, 2015 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.