why is "this" keyword used within the setAdapter method in this snippet ?What does it refer?
GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this));
The typical use of this keyword is that it refers to the current ob开发者_运维技巧ject,but in this example what it refers?
Your code here:
GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this));
It is show really clear that new ImageAdapter(this) is need an arg : this or getApplicationContext or Context or YourCurrentClassName.this
this = getApplicationContext or Context or YourCurrentClassName.this and The ImageAdapter need is arg to pass into itself class
In this example this refers to the class in which this piece of code is written
I suppose i get this code from here http://developer.android.com/resources/tutorials/views/hello-gallery.html as you can see ImageAdapter extends BaseAdapter and ImageAdapter is accepting Context as parameter in constructor so this refer to the context in which you are calling this code (in most cases this will be your Activity).
精彩评论