开发者

click listener for button inside list view in android

开发者 https://www.devze.com 2023-04-06 06:16 出处:网络
In my android app I have a list and in each row I have a button. On pressing the button, another activity should open.开发者_如何学JAVA I am a little confused how to do the click listener. Can anyone

In my android app I have a list and in each row I have a button. On pressing the button, another activity should open.开发者_如何学JAVA 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.


where ever you are inflating the row view, get the reference to the button in the listItem, and add clickListener to it. You set the listener by

button.setOnClickListener()

and in the listener click call new activity.

declare a field your activity class like this-

private Context mCurrentContext = this;

and when you call the new Activity,

mCurrentContext.startActivity(Intent, int);
0

精彩评论

暂无评论...
验证码 换一张
取 消