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 :(
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With