Extending Generics with nested types
-
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 calledEntry
-- think JournalEntry). TheAdapter
you create will contain a list of your Model :List allEntries;
But, here is where it starts getting weird: Your
EntryAdapter
has to extend ARecyclerView.Adapter
ofEntryAdapter.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 yourEntryAdapter
contains a class namedViewHolder
andEntryAdapter.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 followingpublic 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
-
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 calledEntry
-- think JournalEntry). TheAdapter
you create will contain a list of your Model :List allEntries;
But, here is where it starts getting weird: Your
EntryAdapter
has to extend ARecyclerView.Adapter
ofEntryAdapter.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 yourEntryAdapter
contains a class namedViewHolder
andEntryAdapter.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 followingpublic 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
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
-
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
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:
-
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 calledEntry
-- think JournalEntry). TheAdapter
you create will contain a list of your Model :List allEntries;
But, here is where it starts getting weird: Your
EntryAdapter
has to extend ARecyclerView.Adapter
ofEntryAdapter.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 yourEntryAdapter
contains a class namedViewHolder
andEntryAdapter.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 followingpublic 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
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
-
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
-
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:
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