Passing variables to a ViewBinder
I have a ViewBinder
that is acting as the middleman between my ListView
and a cursor adapter to a database. Depending on the context this data is being displayed in, I need the ViewBinder
to do different things.
As an example, take a task management application, that is displaying tasks for all of the task groups that exist. Maybe in this case the app wants to display the name of the task group in the list view, when it wouldn't want to if it was showing tasks for one of the specific task groups. The list view item could have a hidden field, and the ViewBinder
can be used to map the t开发者_运维知识库ask group's name to the field AND set it to visible when necessary (as instructed the main application).
My question is how would one tell the ViewBinder
the context in which it is displaying, so it can determine how to behave?
I realize this can likely be done by implementing many different ViewBinders
, but this would require much code to be duplicated, and would prefer doing it with a single ViewBinder
that I just pass certain parameters to.
This can be done by implementing the constructor for the ViewBinder you are implementing.
Per the example, one can do something like this:
private Boolean displayGroupName = true;
public ToDoViewBinder(Boolean displayGroupName) {
this.displayGroupName = displayGroupName;
}
@Override
public boolean setViewValue(View view, Cursor c, int columnIndex) {
if(displayGroupName)
{
//Do necessary stuff
return true;
}
else
return false;
}
精彩评论