1
votes

I am new to Xamarin. I am trying to bind an object to AutocompleteTextView in Xamarin Android. I am able to attach the adapter but when I type in text in the AutoCompleteTextView the object is displayed as is and not the text. I added Event Handler for Item Click and able to get the value that i want to display. Below is my code and any help or pointers on how to get to display the value as suggestions.

Main Activity

public class MainActivity : Activity { int count = 1;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);

        //button.Text = 
        MainActivityModel Mv = new MainActivityModel();
        ArrayAdapter<AutoCompleteTextModel> adapter = new ArrayAdapter<AutoCompleteTextModel>  
            (this,Android.Resource.Layout.SimpleDropDownItem1Line,Mv.StationsList);

        AutoCompleteTextView actv = FindViewById<AutoCompleteTextView>(Resource.Id.source);

        actv.Adapter = adapter;
        //actv.Threshold = 1;

        actv.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
            actv.Text = Mv.StationsList[e.Position].StationName;
        }; 

        actv.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {

            new ArrayAdapter (
                this, 
                Android.Resource.Layout.SimpleListItem1, 
                Mv.StationsList.FindAll (a => a.StationName.Contains(actv.Text)).ToArray ());
        };


        button.Click += delegate {
            button.Text = string.Format ("{0} clicks!", count++);
        };
    }
}

AutoCompleteTextViewModel

public class AutoCompleteTextModel { public string StationName { get; set; } public int StationId { get; set; }

    public AutoCompleteTextModel ()
    {
    }
}

public class MainActivityModel { public List StationsList;

    public MainActivityModel ()
    {
        StationsList = new List<AutoCompleteTextModel> ();

        StationsList.Add (new AutoCompleteTextModel () {StationId = 1, StationName = "Chennai Beach"
        });
        StationsList.Add (new AutoCompleteTextModel () {StationId = 1, StationName = "Chennai Fort"
        });
        StationsList.Add (new AutoCompleteTextModel () {StationId = 1, StationName = "Chennai Park"
        });
        StationsList.Add (new AutoCompleteTextModel () {StationId = 1, StationName = "Chennai Egmore"
        });
        StationsList.Add (new AutoCompleteTextModel () {StationId = 1, StationName = "Chetpet"
        });
}

}

1

1 Answers

2
votes

enter image description here

_objGeneralDetailProductType is the objecttype of AutoCompleteTextView

You can try this to bind the list in Auto Complete because in my hand, i am using it and it is working fine.