Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does AdapterView<?> mean in the OnitemClick() Method? What is the use of other parameters in it?

People also ask

What is AdapterView?

AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source.

What is OnItemClickListener in Android?

android.widget.AdapterView.OnItemClickListener. Known indirect subclasses. CharacterPickerDialog, PreferenceScreen. CharacterPickerDialog. Dialog for choosing accented characters related to a base character.

Can be used to provide views for an AdapterView?

An AdapterView is a group of widgets (aka view) components in Android that include the ListView, Spinner, and GridView. In general, these are the widgets that provide the selecting capability in the user interface (read about AdapterView widgets here).

What are the different types of adapters in Android?

Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are ArrayAdapter,Base Adapter, CursorAdapter, SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter.


The <?> indicates a Generic. Read more about them here.

Here is what the documentation says about the parameters:

onItemClick(AdapterView<?> parent, View view, int position, long id)

parent The AdapterView where the click happened.

view The view within the AdapterView that was clicked (this will be a view provided by the adapter)

position The position of the view in the adapter.

id The row id of the item that was clicked.

The AdapterView could be a ListView, GridView, Spinner, etc. The question mark inside the angle brackets indicates that it could be any of them. This is called generics in Java. You can use parent in code to do something to the whole view. For example, if you were using a ListView you could hide the whole ListView by the following line of code:

parent.setVisibility(View.GONE);

The View refers to a specific item within the AdapterView. In a ListView it is the row. Thus, you can get a reference to a TextView within a row by saying something like this:

TextView myTextView = (TextView) view.findViewById(R.id.textView1);
String text = myTextView.getText().toString();

The position is the position of the view in the parent. For a ListView, it is the row number. The top row is position 0, the second row is position 1, the third row is position 2, etc. Note that if your ListView has a header view (like if you did ListView.addHeaderView(View)) then the header view would be position 0 and the actual rows would start their numbering at 1.

Sometimes id is the same as position and sometimes it is different. If you are using an ArrayAdapter or SimpleAdapter then they are the same (except in the case that there is a header view and then they are off by one). For a CursorAdapter (and consequently a SimpleCursorAdapter) the id returns the row id of the table, that is, _id.

Here are a few other good answers on this topic:

  • https://stackoverflow.com/a/9863279/3681880
  • https://stackoverflow.com/a/12966006/3681880
  • https://stackoverflow.com/a/24531354/3681880