Monday, May 14, 2012

A MonoDroid MvvmCross experiment in autocomplete


I got asked a question about implementing Mono for Android/MonoDroid autocomplete support on Jabbr - http://jabbr.net/#/rooms/mvvmcross
After some hacking at a Google Books API sample, then I created a sample - see the video at:
This example works using a new alpha databinding Autocomplete class and adaptor within the MvvmCross framework. It may be that these classes never actually make the cut to be full time framework members - in which case they can live in some external library instead.
The basic functionality uses databinding on 3 new properties:
  • PartialText - which is a partial text string - sent from the View to the ViewModel
  • ItemsSource - which is the set of current items available for the supplied PartialText - sent from the ViewModel to the View
  • SelectedObject - which is the current selected item - sent from the View to the ViewModel
You can see these setup in the binding xml as:
<Mvx.MvxBindableAutoCompleteTextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  local:MvxItemTemplate="@layout/listitem_book"
  local:MvxBind="{'Text':{'Path':'EnteredText','Mode':'TwoWay'},
  'ItemsSource':{'Path':'AutoCompleteSuggestions'},
  'PartialText':{'Path':'CurrentTextHint'},
  'SelectedObject':{'Path':'CurrentBook'}}"
    />
Note that because of the Android threading model it is essential that every change in PartialText is met by an eventual signalled change in ItemsSource - and this should be a single change in object collection rather than lots of small changes.
Note that this sample uses "simple binding" rather than the full Mvx framework and as a result there is slightly more threading to worry about in the ViewModel.
The binding view and its adapter are not simple code to follow - the binding code is fairly abstract in nature - but they can be found in:
One Two Three
If you are doing anything network-linked then in the long term I believe it may be better to implement a new autocomplete view rather than to use the built into Android today!
And if you are doing an autocomplete sample, then I highly recommend the google books api - it's simple to use and fab for demos :)

No comments:

Post a Comment