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

2

32
gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
Acropetal answered 6/7, 2010 at 8:35 Comment(0)
H
32

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:

Hacker answered 6/7, 2010 at 8:35 Comment(1)
How to get the id of AutoCompleteTextView using parent object?Hazelwood
M
0

The ? means that the datatype is unknown and it can be any type.

Malda answered 6/7, 2010 at 8:35 Comment(1)
You added the adapter and know the type - so simply typecast to get what u want.Across

© 2022 - 2024 — McMap. All rights reserved.