You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In all RecyclerViewAdapter classes we have some common, not needed code. We should refactor it to remove this code.
Initialization
It's always done in three steps:
Create an adapter with the constructor (planListAdapter = new PlanRecyclerViewAdapter(planItemClickListener);)
set adapter in recycle view (recyclerView.setAdapter(planListAdapter);)
set adapter items via setter method (planListAdapter.setPlanItems(planTemplateRepository.getAll());)
We can pass data in constructor to avoid initialization with empty ArrayList and extra setter call:
planListAdapter = new PlanRecyclerViewAdapter(planTemplateRepository.getAll(), planItemClickListener);
recyclerView.setAdapter(planListAdapter);
onBindViewHolder
Currently, we do checking against null or empty values list: (planItemList != null && !planItemList.isEmpty())
Actually we don't need those checks, cause:
value list should never be null (it's inited in the constructor with a list of values from the repo, or updated later via setter with the same kind of data). Setting null value should be considered as programming error and should cause app crash instead of undefined behavior.
If the list is empty there is nothing to display so Android won't call this method
We can just remove those checks
getItemCount
Currently, we do checking against null or empty values list: return planItemList != null && planItemList.size() != 0 ? planItemList.size() : 0;
List won't be null (as described in 2) )
We don't need special treatment for the situation when list.size() == 0 cause return list.size() already resolves it (return list.size() != 0 ? list.size() : 0; is identical to return list.size();
We can refactor it to simply return list.size();
The text was updated successfully, but these errors were encountered:
In all RecyclerViewAdapter classes we have some common, not needed code. We should refactor it to remove this code.
It's always done in three steps:
planListAdapter = new PlanRecyclerViewAdapter(planItemClickListener);
)recyclerView.setAdapter(planListAdapter);
)planListAdapter.setPlanItems(planTemplateRepository.getAll());
)We can pass data in constructor to avoid initialization with empty ArrayList and extra setter call:
onBindViewHolder
Currently, we do checking against null or empty values list:
(planItemList != null && !planItemList.isEmpty())
Actually we don't need those checks, cause:
We can just remove those checks
getItemCount
Currently, we do checking against null or empty values list:
return planItemList != null && planItemList.size() != 0 ? planItemList.size() : 0;
list.size() == 0
causereturn list.size()
already resolves it (return list.size() != 0 ? list.size() : 0;
is identical toreturn list.size();
We can refactor it to simply
return list.size();
The text was updated successfully, but these errors were encountered: