views:

271

answers:

3

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?

A: 

Edit: I think this might answer your question: http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons

Brandon
+2  A: 

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.

BrennaSoft
This does work, but why has to be done this way?
Pentium10
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
A: 

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
Yes, I've tried that. No whatsoever activity there.
Pentium10