A Better ArrayAdapter Implementation (Android Development)

Filed in Software Developement 7 Comments

I have been doing some Android development these past few days and coming form C#/WPF land, I have noticed that there are a number of design flaws in the android architecture that I feel could be improved. The most annoying thing that I have run into involves the ArrayAdapter. I use lists a lot (as I’m sure others do to) and having a quick and easy adapter that is generic and extendable is very important. The built in ArrayAdapter has many flaws, the biggest are that you cannot access the backing list (EVER!) and filtering is completely broken. To solve this, most people just create their backing list outside of the adapter and pass it in, which is no big deal but completely unnecessary. And to solve the filtering issue, everyone basically just rewrites the filter themselves (ugh!). Because of the way filtering works, you must maintain two copies of your list. One copy contains all items, and the other the filtered items. Without having easy access to the backing list, the filtering ops start to become very inefficient (requirements of copying items back and forth). ArrayAdapter fixes this by doing it for you but only if your list is of String’s (WAT?).

So anyways, i got fed up with rewriting nearly all the ArrayAdapter code every time i wanted to have a custom adapter, i decided to just sack up and write a GOOD ArrayAdapter, which I am calling the DynamicArrayAdapter. This new adapter handles changes much more efficiently and MASSIVELY simplifies any customizations of the adapter. If you compare it with the original ArrayAdapter, you’ll see that it is not very different at all, but the differences MAKE THE DIFFERENCE. So here is some code: http://pastebin.com/4mxtEC5e http://pastebin.com/nDJNuuxk (Now with BSD license)

Here is a sample implementation (for a sample Bluetooth app I wrote): http://pastebin.com/Quwsgkbm

I fully expect to never use ArrayAdapter again.

, , , ,

TOP