• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps <?> Code Explanation

mvmd

Lurker
Here's the code, and what I need help understanding is the purpose of
PHP:
<?>

I see it in tutorials but I can't seem to connect the dots on what it does

PHP:
public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	      // When clicked, show a toast with the TextView text
	      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
	          Toast.LENGTH_SHORT).show();
	    }
 
hi mvmd,

this is a design pattern called Generics, generics are used when someone wants to make an object that works on more than one object.
for example the ArrayList<?> class in java is is type of array that takes objects. but it doesn't care which object it is
when you first call the ArrayList, lets say for example:
Code:
ArrayList<Integer> example_array = new ArrayList<Integer>();
i would now have an ArrayList that holds Integers.

i could just as well call up:
Code:
ArrayList<String> example_array2 = new ArrayList<String>();
and get an Array that only holds strings.
i would recommend looking up java generics as it's something you'll probably use a lot :)
also, try looking for 'java generics wildcard' - this is the question mark you saw, and it basically means "this will accept any object".

hope this helped!

-sump
 
Back
Top Bottom