3

I have a difficulty in understanding following method. In the documentation, the method description is as follows:

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


Parameters:
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.

I understand last two, but couldn't understand what parent does here and why view is required?

if someone have a good explanation, then please let me understand.

Community
  • 1
  • 1
xyz
  • 1,325
  • 5
  • 26
  • 50

1 Answers1

6

The AdapterView could be a ListView, GridView, Spinner, etc. 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();
Nadir Belhaj
  • 11,953
  • 2
  • 22
  • 21
  • thanks..now I understood the parent.can you write a practical example about second one? – xyz Sep 14 '14 at 10:51
  • @logx what about this one ? TextView myTextView = (TextView) view.findViewById(R.id.textView1); String text = myTextView.getText().toString(); – Nadir Belhaj Sep 14 '14 at 10:53
  • so, what it does? Is it for taking reference of textview "within" the listview? – xyz Sep 14 '14 at 10:55
  • 2
    yes it's a Child view inside the parent so the parent is the bigger view like the listView and it's child is the smaller View like a TextView or ImageView inside the parent which is the ListView in this example – Nadir Belhaj Sep 14 '14 at 10:56
  • thanks...so one more doubt: we require AdapterView because we combine child view elements in one parent view element. exactly? – xyz Sep 14 '14 at 10:58
  • 1
    yep exactly an AdapterView it's for combining lots of Views in one bigger View but depending on the purpose that's why sometimes we use listview and in other times GridView etc... – Nadir Belhaj Sep 14 '14 at 10:59