Search code examples
mvvmcrossautocompletetextview

MvxAutoCompleteTextView setting Text property to SelectedObject ToString()


I am using MvxAutoCompleteTextView (MVVM Cross's custom AutoCompleteTextView) and have the List appearing fine and the ItemTemplate displaying as expected.

When I click on one of the Items in the List the Text property gets set to the full name of the Type of object in the List. For example if the List contained objects of type MyObject in a namespace of MyCompany.MyDept the text property would be set to the string "MyCompany.MyDep.MyObject"

Anyone else ever seen this?

UPDATE It looks like Android's AutoCompleteTextView prefers just a list of strings as the source of the list. There is a method in the Android code called ConvertSelectionToStringFormatted but I cannot see how to provide an alternative to that


Solution

  • If you look at my UPDATE in the question you will see that the problem lay with ConvertSelectionToStringFormatted.

    I could not see how to easily create a custom version of MvxAutoCompleteTextView with my own implemenation of ConvertSelectionToStringFormatted so I need a different approach.

    Android's AutoCompleteTextView was obviously calling ToString on the selected object so I overrode ToString in my object to return a display name that was more useful than "MyCompany.MyDep.MyObject"

    I thought I would also include my final axml for the control as that was pretty important

    <MvxAutoCompleteTextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:completionThreshold="1"
                        android:hint="Enter drug name..."
                        local:MvxItemTemplate="@layout/item_myObj"
                        local:MvxBind="ItemsSource Suggestions; 
                                       PartialText SearchTerm; 
                                       SelectedObject SelectedObj;" />
    

    Setting completionThreshold was pretty important, when that was not set the control stopped working\searching once I had cleared the box. No matter what I typed after clearing the box (with backspace) it would not autocomplete anymore. Another odd issue when threshold was not set was that the PartialText was binding as an empty string once it went to 1 character! Yes 1 not 0.

    Do not make the mistake of binding the Text property of the control. PartialText is the search term leave Text well alone. This caused me lots of odd issues.

    Good luck