开发者

Creating a split button for a custom ListView adapter

I'd like to make a list adapter that formats views like this:

Creating a split button for a custom ListView adapter

I want to be able to fire a different onClick when the user clicks the image. I have defined the onClick on the image itself in the getView() override, but how do I then get the position of the line that was clicked so I can update the record in the database to record the acti开发者_如何学编程on?


First, you need the ListView which represents the adapter. If you store this somewhere already, great; if not, you can take the View which is passed to onClick() and call its getParent() method twice (or more, if the image is nested deeper within the clicked item's view) to get the ListView.

From there, call ListView.getPositionForView() on the View passed into onClick(). This will give you an int representing the position of the clicked item within the list adapter. From there, you can do whatever you want with it.

For example:

public void onClick(View v){
    ListView lv = (ListView)(v.getParent().getParent()); // you may need more getParent()s and/or extra casting
    int position = lv.getPositionForView(v);
    /* Do whatever database stuff
     * You want to do
    */
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜