Java / Android auto-resolving generic type. Asking for step-by-step guidance to understand
NickName:Christopher Rucinski Ask DateTime:2013-09-26T14:39:22

Java / Android auto-resolving generic type. Asking for step-by-step guidance to understand

I was recently working on some Android code (this should be a generic Java question - pardon the pun - about generics and Java auto resolving the type).

Here is my code (Android-based, but please create a java specific version if you need to)

class ViewFinder<T extends View> {

    @SuppressWarnings("unchecked")
    final static <T> T byId(View view, int resource) {

        return (T) view.findViewById(resource);


    }

}

Here is the interesting bit...

// Convert XML UI component definitions into the static View Holder object

// Here is what we normally have to do for Android to convert the XML into a UI component
holder.txtGroupName = (TextView) row.findViewById(R.id.txtGroupName);

// This is what I can do with my ViewFinder class above!        
holder.txtGroupName = ViewFinder.byId(row, R.id.txtGroupName);

// This is what I was EXPECTING to do with my ViewFinder class above!       
holder.txtGroupName = ViewFinder<TextView>.byId(row, R.id.txtGroupName);

No I know that Java (not Android) is resolving the Generic T type to be a TextView UI component, but why and *how*?

Can I get a "play-by-play" of what is going on here? I want to be able to figure out if this is going to happen before I code something next time.

Copyright Notice:Content Author:「Christopher Rucinski」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19021095/java-android-auto-resolving-generic-type-asking-for-step-by-step-guidance-to

More about “Java / Android auto-resolving generic type. Asking for step-by-step guidance to understand” related questions

Java / Android auto-resolving generic type. Asking for step-by-step guidance to understand

I was recently working on some Android code (this should be a generic Java question - pardon the pun - about generics and Java auto resolving the type). Here is my code (Android-based, but please ...

Show Detail

Android bluetooth remote control guidance for a project

I'm in a dire need for some guidance on how to build an android bluetooth remote control. I understand that two things would be needed. A server that will take request from the android device via

Show Detail

Generic Class Java - how to understand?

friends! I decided to read more about generics and I am stuck in wiki. A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the...

Show Detail

How to get generic container class of a ParameterizedType in java

Every questions about the java reflection on generic types I found, were asking about getting parameters of a generic type. In my case, I have a ParameterizedType and want to get the Generic contai...

Show Detail

What are generic arrays in Java?

I have a code, whose MCVE is like this import java.util.LinkedList; public class Test { LinkedList&lt;Node&gt;[] arr; Test(){ arr = new LinkedList&lt;Node&gt;[4]; }

Show Detail

Instantiating generic objects in Java

Is it possible to instantiate generic objects in Java as does in the following code fragment? I know that it is possible in C#. But, I haven not seen a similar mechanism yet in Java. // Let T be a

Show Detail

Which Java generic should be used in ambiguous cases?

I'm having some problems with a Wicket 1.3 -> Wicket 1.4 migration, but this question could be applied to Java generics overall, too. The migration has caused hundreds of warnings to spring up out...

Show Detail

How Connect to Remote MySQL server through Android App via JDBC

I first successfully connected via JDBC to a remote MySQL server with a Java program. That I understand how to do. My goal is do the same thing with an Android application and display data from a r...

Show Detail

Associating a generic type with Enum in Java

I am creating a store for user preferences, and there are a fixed number of preferences that users can set values for. The names of the preferences (settings) are stored as an Enum: public enum

Show Detail

Image layering advice/guidance in java

Hi I want to start creating my app but need some guidance. I have several images that i have designed and would like to layer these images. lets say I have image A, B and C. i want to layer image B...

Show Detail