Click here to Skip to main content
14,269,076 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Why Microsoft Disgusts Me Today Pin
Dave Kreskowiak22-Mar-19 4:07
mveDave Kreskowiak22-Mar-19 4:07 
GeneralRe: Why Microsoft Disgusts Me Today Pin
MacSpudster26-Mar-19 10:50
professionalMacSpudster26-Mar-19 10:50 
GeneralRe: Why Microsoft Disgusts Me Today Pin
abmv8-Apr-19 21:38
professionalabmv8-Apr-19 21:38 
GeneralRe: Why Microsoft Disgusts Me Today Pin
Rick York9-Apr-19 5:20
mveRick York9-Apr-19 5:20 
General#define Macros and the Preprocessor Used by the Resource Compiler Pin
David A. Gray10-Mar-19 10:38
groupDavid A. Gray10-Mar-19 10:38 
GeneralRe: #define Macros and the Preprocessor Used by the Resource Compiler Pin
Rick York12-Mar-19 10:56
mveRick York12-Mar-19 10:56 
GeneralRe: #define Macros and the Preprocessor Used by the Resource Compiler Pin
Dr.Walt Fair, PE13-Mar-19 6:27
professionalDr.Walt Fair, PE13-Mar-19 6:27 
GeneralExtending Generics with nested types Pin
raddevus4-Mar-19 10:30
mvaraddevus4-Mar-19 10:30 
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<Entry> 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<EntryAdapter.ViewHolder>

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<EntryAdapter.ViewHolder>{
    public List<Entry> allEntries;
    
    public EntryAdapter() {

    }
    public EntryAdapter(List<Entry> 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<EntryAdapter.ViewHolder>{
    public List<Entry> allEntries;
    
    public EntryAdapter() {

    }

    public EntryAdapter(List<Entry> 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 deprecated.
2. Generic / template type syntax is always difficult to read.
Here's my natural language reading of my EntryAdapter type:
I have an EntryAdapter that is a RecyclerView adapter of EntryAdapter viewholder. WTF | :WTF:
3. This kind of thing makes it kind of difficult to talk about the types and the code and ends up causing me just to memorize all of this. Maybe that is good but it's quite an extensive pattern just to get to a listview of items.

My brain hurts. But I did get it working at least.
GeneralRe: Extending Generics with nested types Pin
Nathan Minier5-Mar-19 2:17
professionalNathan Minier5-Mar-19 2:17 
GeneralRe: Extending Generics with nested types Pin
raddevus5-Mar-19 2:33
mvaraddevus5-Mar-19 2:33 
GeneralRe: Extending Generics with nested types Pin
Dr.Walt Fair, PE13-Mar-19 6:28
professionalDr.Walt Fair, PE13-Mar-19 6:28 
GeneralRe: Extending Generics with nested types Pin
raddevus13-Mar-19 7:30
mvaraddevus13-Mar-19 7:30 
GeneralRe: Extending Generics with nested types Pin
Dr.Walt Fair, PE7-May-19 5:50
professionalDr.Walt Fair, PE7-May-19 5:50 
GeneralDocumentation boggle of the day Pin
Gary Wheeler6-Feb-19 8:22
memberGary Wheeler6-Feb-19 8:22 
GeneralRe: Documentation boggle of the day Pin
MarkTJohnson6-Feb-19 8:37
professionalMarkTJohnson6-Feb-19 8:37 
GeneralRe: Documentation boggle of the day Pin
Ron Anders6-Feb-19 10:20
memberRon Anders6-Feb-19 10:20 
GeneralRe: Documentation boggle of the day Pin
pwasser6-Feb-19 12:53
memberpwasser6-Feb-19 12:53 
GeneralRe: Documentation boggle of the day Pin
Jörgen Andersson6-Feb-19 18:08
protectorJörgen Andersson6-Feb-19 18:08 
GeneralRe: Documentation boggle of the day Pin
pwasser6-Feb-19 18:24
memberpwasser6-Feb-19 18:24 
GeneralRe: Documentation boggle of the day Pin
Jörgen Andersson6-Feb-19 19:52
protectorJörgen Andersson6-Feb-19 19:52 
GeneralRe: Documentation boggle of the day Pin
Kirk 103898217-Feb-19 2:56
memberKirk 103898217-Feb-19 2:56 
GeneralRe: Documentation boggle of the day Pin
David O'Neil6-Feb-19 17:20
professionalDavid O'Neil6-Feb-19 17:20 
GeneralRe: Documentation boggle of the day Pin
Nelek6-Feb-19 21:05
protectorNelek6-Feb-19 21:05 
GeneralRe: Documentation boggle of the day Pin
Mark_Wallace6-Feb-19 21:33
memberMark_Wallace6-Feb-19 21:33 
GeneralRe: Documentation boggle of the day Pin
Gary Wheeler7-Feb-19 1:29
memberGary Wheeler7-Feb-19 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.