I have some values coming from a datasource that I have control over. I am stuffing those values into a ListView. The values go something like this:
"You have X books overdue". This is a silly example but it gets the point I’m trying to understand. Everything EXCEPT X is static however when the ArrayAdapter populates the ListView the only thing that shows is X. This of course make sense.
I can think of two ways to solve this problem. One is to modify my data source so the entire message is provided; this makes life simple on the GUI side but my database colleagues are definitely going to frown.
On the other hand I can find the TextView by id and concatenate EXCEPT ListView is just a representation of the underlying data so really I would modify the data AFTER getting it from the db then update / redraw the list view?
Attach a Binder to your list adapter and modify the data that gets set into it. Assuming you have an adapter called
myAdapter:When you reach the
ViewBinderin your code, the program is trying to populate your list. You’d then check if the view is as instance ofTextViewand if so, using the variabletextwhich in this case is the element from position X of your data source, we create a String with format including the source data.If you have multiple views being populated for each row, you can check the id with
view.getId()Without a ViewBinder, your data gets mapped to the views as specified on the adapter, but with the ViewBinder in place you can do all sorts of cool stuff like, alternative background color for the rows or making a view clickable depending on the content of the source, the possibilities are endless.