gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Share
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)The
AdapterViewcould be aListView,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 aListViewyou could hide the wholeListViewby the following line of code:The
Viewrefers to a specific item within theAdapterView. In aListViewit is the row. Thus, you can get a reference to aTextViewwithin a row by saying something like this: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 yourListViewhas a header view (like if you didListView.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
ArrayAdapterorSimpleAdapterthen they are the same (except in the case that there is a header view and then they are off by one). For aCursorAdapter(and consequently aSimpleCursorAdapter) the id returns the row id of the table, that is,_id.Here are a few other good answers on this topic: