Autocomplete in MVVMCross Xamarin (Android)

I am trying to create an autocomplete element in a Xamarin Android layout. I am using MVVMCross.

I created the following AXML layout in my snippet.

 <TextView
            android:text="Item"
            android:layout_column="0"
            android:id="@+id/textView42"
            android:layout_height="28.6dp"
            android:layout_width="86.9dp"
            android:gravity="center"
            android:layout_marginTop="17.5dp"
            android:layout_marginRight="17.5dp" />
        <AutoCompleteTextView
            android:id="@+id/autocomplete_country"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            local:MvxBind="Adapter Items" />

      

I have updated my view model to return a string array.

private string[] _items = new string[] { "DD", "DD2" };
public String[] Items
{
    get { return this._items; }
    set { this._items = value; RaisePropertyChanged<string[]>(() => this._items); }
}

      

I think I need to use ArrayAdapter, but I'm not sure how to do this. Please provide some directions / pointers to help me proceed.

I'm new to Xamarin and MVVMCross so maybe everything is missing here.

+3


source to share


1 answer


You don't need a special adapter. Switch to MvxAutoCompleteTextView and use MVVMCross binding. Here's an example of how I used it.

<MvxAutoCompleteTextView
    android:id="@+id/DrugName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:completionThreshold="1"
    android:hint="Enter drug name..."
    android:textSize="@dimen/text_size_xlarge"
    local:MvxItemTemplate="@layout/item_drug_notclickable"
    local:MvxBind="ItemsSource DrugSuggestions; PartialText DrugSearchTerm; SelectedObject Drug;"
    android:layout_gravity="right" />

      



As Nicola said in the comments, you want to call RaisePropertyChanged on the Items property, not a private variable. This applies to all modified calls.

One thing to keep in mind when using autocomplete is that changes to the text should cause the ItemSource to change. Take a look at this GitHub answer for a complete explanation https://github.com/MvvmCross/MvvmCross/issues/945 received

+2


source







All Articles