I am learning Android apps development. I started by reading the API Guide (I had some background before) and when I came at Layouts section I found the following:
// Create a message handling object as an anonymous class.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Do something in response to the click
}
};
listView.setOnItemClickListener(mMessageClickedHandler);
where:
1- listView is a ListView object which is subclass of AdapterView.
2- OnItemClickListener is a nested class (interface) in AdapterView.
when I first read that, I noticed it didn't write the anonymous class name as: AdapterView.OnItemClickListener ...
I thought that's maybe because it assumes the code is in an AdapterView class, but when I noticed the last line listView.setOnItemClickListener(mMessageClickedHandler);
I said this must be in an Activity method, I finally concluded that it must be a mistake.
But when I reached Grid View guide (https://developer.android.com/guide/topics/ui/layout/gridview.html) and in step no. 4, it writes this snippet:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(HelloGridView.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
which also initiate a new class using OnItemClickListener()
not AdapterView.OnItemClickListener()
, and now it is clearly in "onCreate()" method.
I said to my self maybe it is okay to use it like this because we are putting it as an argument to a method in an object that "OnItemClickListener" is defined in.
(I am an electronics engineer by the way, not specialized programmer, so I thought I missed that when I learnt Java)
but when I came to the Toggle Button lesson, I saw this code snippet:
(Link: https://developer.android.com/guide/topics/ui/controls/togglebutton.html)
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
where it has the same conditions for the previous example, but when it used the nested interface it wrote it with the class containing that interface: CompoundButton.OnCheckedChangeListener()
instead of: OnCheckedChangeListener()
as the previous example.
It has the same conditions of the previous one because:
1- CompoundButton and AdapterView both contained a nested interface.
2- ToggleButton and GridView are both subclasses of CompoundButton and AdapterView, respectively.
3- They are both defined and used in the same scope, i.e. in an activity method.
So, I went to search about the lesson of the GridView to see if someone had problems with it, I found the following question:
And he used the same code in the GridView lesson above, and it worked with him!
(after he imported the appropriate packages)
So why they are used differently? am I missing something in Java? or I had some mistake in what I noticed?