Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click listener for button inside list view in android

In my android app I have a list and in each row I have a button. On pressing the button, another activity should open. I am a little confused how to do the click listener. Can anyone kindly suggest ? Thanks.

note: i can create a click listened inside the array adapter. However, I am unable to start a new activity from there :(

like image 692
ahsan Avatar asked Feb 15 '26 08:02

ahsan


1 Answers

Put a button in your custom view and handle click event in getView method.

Your code should look something like this.

public View getView(final int position, View convertView,ViewGroup parent) 
{
   if(convertView == null)
   {
        LayoutInflater inflater = getLayoutInflater();
        convertView  = (LinearLayout)inflater.inflate(R.layout.YOUR_LAYOUT, null);
   }

   Button yourButton= (Button)  convertView  .findViewById(R.id.YOUR_BUTTON_ID);

   yourButton.setOnClickListener(new OnClickListener() 
   { 
       @Override
       public void onClick(View v) 
       {
           // Your code that you want to execute on this button click
           Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
           CurrentActivity.this.startActivity(myIntent);

       }

   });    
   return convertView ;
}

Hope this helps.

like image 78
Badal Avatar answered Feb 20 '26 17:02

Badal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!