Pass in the object a java class is embedded in as a parameter.

Posted by Leif Andersen on Stack Overflow See other posts from Stack Overflow or by Leif Andersen
Published on 2010-04-19T03:01:20Z Indexed on 2010/04/19 3:03 UTC
Read the original article Hit count: 260

Filed under:
|
|
|

I'm building an android application, which has a list view, and in the list view, a click listener, containing an onItemClick method. So I have something like this:

public class myList extends ListActivity {
    @Override
public void onCreate(Bundle savedInstanceState) {
        getListView().setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
            /* Do something*/
        }
    }
}

Normally, this works fine. However, many times I find myself needing too preform an application using the outer class as a context. thusfar, I've used:

parent.getContext();

to do this, but I would like to know, is that a bad idea? I can't really call:

super

because it's not really a subclass, just an embedded one. So is there any better way, or is that considered cosure? Also, if it is the right way, what should I do if the embedded method doesn't have a parameter to get the outside class?

Thank you.

© Stack Overflow or respective owner

Related posts about android

Related posts about java