Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Extending Generics with nested types

Extending Generics with nested types

Scheduled Pinned Locked Moved The Weird and The Wonderful
androidtutorial
6 Posts 3 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • raddevusR Offline
    raddevusR Offline
    raddevus
    wrote on last edited by
    #1

    The new way to make a ListView in Android apps is to use the RecyclerView. It's quite a complex thing, just to create a ListView. You will have to create an Adapter class based off your Model type (in my case and the example below that Model type is called Entry -- think JournalEntry). The Adapter you create will contain a list of your Model :

    List allEntries;

    But, here is where it starts getting weird: Your EntryAdapter has to extend A RecyclerView.Adapter of EntryAdapter.Viewholder : So your class definition will look like the following:

    public class EntryAdapter extends RecyclerView.Adapter

    It Gets Weirder And it gets weirder (to me). That Generic type (RecyclerView.Adapter) expects that your EntryAdapter contains a class named ViewHolder and EntryAdapter.ViewHolder extends RecyclerView.ViewHolder. So here is just the basic outline of your EntryAdapter:

    public class EntryAdapter extends RecyclerView.Adapter{
    public List allEntries;

    public EntryAdapter() {
    
    }
    public EntryAdapter(List entryList) {
        allEntries = entryList;
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder {
    
    }
    

    }

    My Head Explodes For me, my head kind of explodes on all of that. The entire reason you need to do all of that is because you need to override a method and implement a couple of methods from the Generic RecyclerView.Adapter. So really, your basic class will look like the following

    public class EntryAdapter extends RecyclerView.Adapter{
    public List allEntries;

    public EntryAdapter() {
    
    }
    
    public EntryAdapter(List entryList) {
        allEntries = entryList;
    }
    
    public EntryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    }
    
    @Override
    public void onBindViewHolder(EntryAdapter.ViewHolder holder, int position) {
    }
    
    public int getItemCount() {
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder {
    
    }
    

    }

    A Few of Things That Stand Out 1. This really is the new expected way to simply create listview type of items in the Android dev world. The ListView type is being dep

    N D 2 Replies Last reply
    0
    • raddevusR raddevus

      The new way to make a ListView in Android apps is to use the RecyclerView. It's quite a complex thing, just to create a ListView. You will have to create an Adapter class based off your Model type (in my case and the example below that Model type is called Entry -- think JournalEntry). The Adapter you create will contain a list of your Model :

      List allEntries;

      But, here is where it starts getting weird: Your EntryAdapter has to extend A RecyclerView.Adapter of EntryAdapter.Viewholder : So your class definition will look like the following:

      public class EntryAdapter extends RecyclerView.Adapter

      It Gets Weirder And it gets weirder (to me). That Generic type (RecyclerView.Adapter) expects that your EntryAdapter contains a class named ViewHolder and EntryAdapter.ViewHolder extends RecyclerView.ViewHolder. So here is just the basic outline of your EntryAdapter:

      public class EntryAdapter extends RecyclerView.Adapter{
      public List allEntries;

      public EntryAdapter() {
      
      }
      public EntryAdapter(List entryList) {
          allEntries = entryList;
      }
      
      public class ViewHolder extends RecyclerView.ViewHolder {
      
      }
      

      }

      My Head Explodes For me, my head kind of explodes on all of that. The entire reason you need to do all of that is because you need to override a method and implement a couple of methods from the Generic RecyclerView.Adapter. So really, your basic class will look like the following

      public class EntryAdapter extends RecyclerView.Adapter{
      public List allEntries;

      public EntryAdapter() {
      
      }
      
      public EntryAdapter(List entryList) {
          allEntries = entryList;
      }
      
      public EntryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      }
      
      @Override
      public void onBindViewHolder(EntryAdapter.ViewHolder holder, int position) {
      }
      
      public int getItemCount() {
      }
      
      public class ViewHolder extends RecyclerView.ViewHolder {
      
      }
      

      }

      A Few of Things That Stand Out 1. This really is the new expected way to simply create listview type of items in the Android dev world. The ListView type is being dep

      N Offline
      N Offline
      Nathan Minier
      wrote on last edited by
      #2

      That's not too bad as far as infrastructure support goes. If you want a real nightmare, have a look at the Zend Framework someday. It will test your sanity. I haven't written Java in years, but can you not define a generic version of the Adapter?

      public class ModelAdapter extends RecyclerView.Adapter.ViewHolder>{
      public List allItems;
      ...
      }

      Or are the needs of that overriden method too specific?

      "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

      raddevusR 1 Reply Last reply
      0
      • N Nathan Minier

        That's not too bad as far as infrastructure support goes. If you want a real nightmare, have a look at the Zend Framework someday. It will test your sanity. I haven't written Java in years, but can you not define a generic version of the Adapter?

        public class ModelAdapter extends RecyclerView.Adapter.ViewHolder>{
        public List allItems;
        ...
        }

        Or are the needs of that overriden method too specific?

        "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

        raddevusR Offline
        raddevusR Offline
        raddevus
        wrote on last edited by
        #3

        Nathan Minier wrote:

        but can you not define a generic version of the Adapter?

        I'm not sure. But since I'm digging through this myself what would that do for me? Are you thinking of creating a generic one that handles multiple types, maybe? I think the only reason this is genericized is because it is an attempt to provide a generic way to bind model elements to the view and since those end up being specific fields then you have to (at this point) give it a specific implementation that you want to bind. Anyways, I think that is the point. Thanks for chiming in on the discussion. :thumbsup:

        1 Reply Last reply
        0
        • raddevusR raddevus

          The new way to make a ListView in Android apps is to use the RecyclerView. It's quite a complex thing, just to create a ListView. You will have to create an Adapter class based off your Model type (in my case and the example below that Model type is called Entry -- think JournalEntry). The Adapter you create will contain a list of your Model :

          List allEntries;

          But, here is where it starts getting weird: Your EntryAdapter has to extend A RecyclerView.Adapter of EntryAdapter.Viewholder : So your class definition will look like the following:

          public class EntryAdapter extends RecyclerView.Adapter

          It Gets Weirder And it gets weirder (to me). That Generic type (RecyclerView.Adapter) expects that your EntryAdapter contains a class named ViewHolder and EntryAdapter.ViewHolder extends RecyclerView.ViewHolder. So here is just the basic outline of your EntryAdapter:

          public class EntryAdapter extends RecyclerView.Adapter{
          public List allEntries;

          public EntryAdapter() {
          
          }
          public EntryAdapter(List entryList) {
              allEntries = entryList;
          }
          
          public class ViewHolder extends RecyclerView.ViewHolder {
          
          }
          

          }

          My Head Explodes For me, my head kind of explodes on all of that. The entire reason you need to do all of that is because you need to override a method and implement a couple of methods from the Generic RecyclerView.Adapter. So really, your basic class will look like the following

          public class EntryAdapter extends RecyclerView.Adapter{
          public List allEntries;

          public EntryAdapter() {
          
          }
          
          public EntryAdapter(List entryList) {
              allEntries = entryList;
          }
          
          public EntryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          }
          
          @Override
          public void onBindViewHolder(EntryAdapter.ViewHolder holder, int position) {
          }
          
          public int getItemCount() {
          }
          
          public class ViewHolder extends RecyclerView.ViewHolder {
          
          }
          

          }

          A Few of Things That Stand Out 1. This really is the new expected way to simply create listview type of items in the Android dev world. The ListView type is being dep

          D Offline
          D Offline
          Dr Walt Fair PE
          wrote on last edited by
          #4

          That almost sounds like a programming discussion in the Lounge.

          CQ de W5ALT

          Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

          raddevusR 1 Reply Last reply
          0
          • D Dr Walt Fair PE

            That almost sounds like a programming discussion in the Lounge.

            CQ de W5ALT

            Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

            raddevusR Offline
            raddevusR Offline
            raddevus
            wrote on last edited by
            #5

            Walt Fair, Jr. wrote:

            That almost sounds like a programming discussion in the Lounge.

            Is this sentence stating your shock? A programming discussion in the lounge!!! What!?! :laugh:

            D 1 Reply Last reply
            0
            • raddevusR raddevus

              Walt Fair, Jr. wrote:

              That almost sounds like a programming discussion in the Lounge.

              Is this sentence stating your shock? A programming discussion in the lounge!!! What!?! :laugh:

              D Offline
              D Offline
              Dr Walt Fair PE
              wrote on last edited by
              #6

              Yeah, but I could be wrong. After all, I'm an engineer who never studied programming in school.

              CQ de W5ALT

              Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups