0

I have an activity, where the ListView holds customized linear layout elements for each row. One of the rows has a button defined as:

 <Button
            android:text="Pick a contact"
            android:id="@+id/btnPickContact"
            android:layout_width="wrap_content"
            android:gravity="center_vertical"
            android:layout_height="wrap_content"></Button>

Then in java, I have this code:

((Button) row.findViewById(R.id.btnPickContact)).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                    intent.putExtra(EXTRA_ONLINE_ID, (String)v.getTag(TAG_ONLINE_ID));
                    act.startActivityForResult(intent, PICK_CONTACT);
                }
            });

In this setup the event fails to start.

Also I've tried by implementing the interface:

@Override
    public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            intent.putExtra(EXTRA_ONLINE_ID, (String)v.getTag(TAG_ONLINE_ID));
            startActivityForResult(intent, PICK_CONTACT);
    }

still no luck, the event doesn't fire.

What to do?

Pentium10
  • 204,586
  • 122
  • 423
  • 502

5 Answers5

2

In the xml defining the button you need to specify a handler using onclick="somehandler". And in your activity have

public someHandler(View v) {
}

v will be the button.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • This is an alternative method for coding the on-click event listener. In effect this dodges the problem, leaving the original problem unsolved. – Brad Hein May 18 '10 at 21:15
0

Edit: I think this might answer your question: Android: ListView elements with multiple clickable buttons

Community
  • 1
  • 1
Brandon O'Rourke
  • 24,165
  • 16
  • 57
  • 58
0

Are you sure its the event that's not firing and not a problem launching the intent?

add log.d("Mytest","OnClick Fired!!!"); as the first line in onClick, then see if you get your message in the debug log.

Brad Hein
  • 10,997
  • 12
  • 51
  • 74
0

You should consider to set clickable and forcusable property to the row layout.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
      //Inflate view ....

      button1.setOnClickListener(onclickListener);    
      convertView.setClickable(true);
      convertView.setFocusable(true);
      return converView;
}
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
-1

try this use this

 (new OnClickListener(){....

instead of this

(new View.OnClickListener(){....

here is code...

 ((Button) row.findViewById(R.id.btnPickContact)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                intent.putExtra(EXTRA_ONLINE_ID, (String)v.getTag(TAG_ONLINE_ID));
                act.startActivityForResult(intent, PICK_CONTACT);
            }
        });
Chirag Patel
  • 11,416
  • 3
  • 26
  • 38