the_machinist_
the_machinist_

Reputation: 455

ImageButton OnClickListener Is Not Firing

I've been pretty confused for a couple days now.

A few days ago I started trying to extend the TableLayout view to make it collapsible. It sounded pretty straight forward.

Problem:

protected void onFinishInflate()
{
    super.onFinishInflate();

    ImageButton ib = (ImageButton) this.findViewById(R.id.collapse);
    // totally gayed
    ib.setOnClickListener(this.onButtonClick(ib));
}

//works
public OnClickListener onButtonClick(ImageButton ib) {
    try {
        if (this.collapsed)
        {
            this.Build();
            ib.setImageResource(android.R.drawable.arrow_up_float);
        } else {
            this.Collapse();
            ib.setImageResource(android.R.drawable.arrow_down_float);
        }
    } catch (Exception e) {
        Log.e("MTK", e.toString() + " : " + e.getMessage() + " : " + e.getCause());
    }
    return null;
}

So the funny thing is that the Table will open with all the rows gone except the header. The onClick method fires when the view is instantiated (alright..?).

But after that I get nothing and I really have no idea what the problem is. I've tried a ton of stuff.

Solution:

public View.OnClickListener onButtonClick(final CollapsibleTableLayout tbl, final ImageButton ib) {
        return new View.OnClickListener() { 
            public void onClick(View v) {
                try {
                    if (tbl.collapsed == true)
                    {
                        tbl.Build();
                        tbl.collapsed = false;
                        ib.setImageResource(android.R.drawable.arrow_up_float);
                    } else {
                        tbl.Collapse();
                        tbl.collapsed = true;
                        ib.setImageResource(android.R.drawable.arrow_down_float);
                    }
                } catch (Exception e) {
                    Log.e("MTK", e.toString() + " : " + e.getMessage() + " : " + e.getCause());
                }
            }
        };
    }

turns out i wasn't returning the click handler. used to them working a little different in other languages.

i thought if i did it that way i wouldnt be able to modify the class i was working in but you just have to pass final references to the click handler.

hope this helps someone

Upvotes: 1

Views: 1140

Answers (1)

Jason Kuang
Jason Kuang

Reputation: 261

I think the problem is that you return null in method onButtonClick, not an valid onClickListener.

Upvotes: 2

Related Questions