This is a migrated thread and some comments may be shown as answers.

Programmatically AddToken in Xamarin Android

1 Answer 75 Views
AutoCompleteTextView - Xamarin.Android
This is a migrated thread and some comments may be shown as answers.
Mayank
Top achievements
Rank 1
Mayank asked on 03 Jan 2019, 10:34 AM

Hello Team,

I am getting Issue while Add Token Programmatically.

I am using RadAutoCompleteTextView for Adding Token Programmatically.

radAutoCompleteTextView.AddToken();

But in This AddToken Method, I have to pass parameter TokenView.  But I don't know how to pass it.

I just want to add one string, like Person Name Programmatically in Token.

I spent many hours but not getting success,
So, if anyone can help me on this, it will be appreciated.

Thanks,

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 03 Jan 2019, 04:54 PM
Hi Mayank,

As the method IntelliSense describes, you need to create a TokenView. Normally, the TokenView is created from the list of TokenModels that the Adapter creates. When the user makes a selection from the filtered list, the AutoCompleteTextView will create a TokenView using that TokenModel and add it to the Tokens collection.

If you follow the Getting Started tutorial for the AutocompleteTextView, you create TokenModels for each of the countries in the data:

private List<TokenModel> GetTokenObjects()
{
    List<TokenModel> feedData = new List<TokenModel>();
 
    foreach (var country in this.countries)
    {
        // Create a TokenModel from one of the items in the data source
        var token = new TokenModel(country, null);
 
        feedData.Add(token);
    }
 
    return feedData;
}


Now that you see where the TokenModels are, you can use this adapter to access the list and get a TokenModel to create a TokenView:

var tokenView = new TokenView(this)
{
    Model = TokenModelToUseForTokenView
};
 
autocomplete.AddToken(tokenView);

Demo

You could also create a new TokenModel from scratch, but keep in mind that it's not guaranteed to be a match in the original data. I've attached a demo, click the Floating Button to add the manual token.

private void FabOnClick(object sender, EventArgs eventArgs)
{
    // Option 1 - Completely make one from scratch
    var tokenModel = new TokenModel($"Token {_autocomplete.Tokens.Count}", null);
 
    // Option 2 - Use one of the items from the data source
    //var tokenModel = autocomplete.Adapter.InitialListOfItems.FirstOrDefault();
 
    // Option 3 - Use one of the items from the data source
    //var tokenModel = autocomplete.Adapter.FilteredList.FirstOrDefault();
 
    var tokenView = new TokenView(this) { Model = tokenModel };
    _autocomplete.AddToken(tokenView);
}




Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
AutoCompleteTextView - Xamarin.Android
Asked by
Mayank
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or