user568658
user568658

Reputation: 1

button in listview

I have listview customized by simplecursoradapter .in each row i have the text and button

I want to click to both the button and list item , but i only click to button so what i should do to to onclicklistener them.

Upvotes: 0

Views: 936

Answers (3)

user1214629
user1214629

Reputation: 11

If you look at the source for the alarm clock app, you will see this functionality in a custom layout that is used for the list item

/**
 * Special class to to allow the parent to be pressed without being pressed
 * itself. This way the time in the alarm list can be pressed without changing
 * the background of the indicator.
 */
public class DontPressWithParentLayout extends RelativeLayout {

public DontPressWithParentLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void setPressed(boolean pressed) {
    // If the parent is pressed, do not set to pressed.
    if (pressed && ((View) getParent()).isPressed()) {
        return;
    }
    super.setPressed(pressed);
}
}

Then they set the clickHandlers for both the list item and the button in the adapter they use.

Upvotes: 1

Vikas Patidar
Vikas Patidar

Reputation: 43359

You need to handle this inside the getView() or bindView() method of your custom SimpleCursorAdapter.

Have a look at the given code snippet:

public View getView(final int pos, View inView, ViewGroup parent)
{
        if (inView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inView = inflater.inflate(R.layout.your_custom_file, null); 
        }
        Button btn = (Button) inView.findViewById(R.id.button);
        btn.setOnClickListener(new OnClickListener() // Click event on button
        {               
            public void onClick(View v) 
            {
                // do what you want when click on button here
            }
        }               
        return inView;
}

Upvotes: 4

RATTLESNAKE
RATTLESNAKE

Reputation: 3228

For listview to be clickable you can use onItemClickListener

Upvotes: -1

Related Questions