Hi guys i wondering if anyody have a example or an idea about populate a spinner like combobox in VB.net/C#.net
Here is my example from sqlite to spinner key/value
List<String> ID; List<String> Name; protected override void OnCreate(Bundle savedInstanceState) { Name = new List<String>(); ID = new List<String>(); db = new SQLiteConnection(dpPath); var table = db.Query<CategoryPreviewClass>("select * from CategoryPreviewClass"); foreach (var item in table) { Name.Add((string)item.CategoryName); ID.Add((string)item.CategoryID); } var adapter0 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, Name); spinner1.Adapter = adapter0; spinner1.ItemSelected += Spinner1_ItemSelected; } private void Spinner1_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e) { string s1 = ID[e.Position].ToString(); Toast.MakeText(this, s1, ToastLength.Short).Show(); }
Answers
You mean this?
Hi Sam.27. thanks for you response i appreciate it.
But im lookin for an example that include KEY/VALUE like DisplayMember/ValueMember in Winforms
IN Xmarin.Android i being use this to populate, but it only contains membersValue
ITEM 1
ITEM 2
ITEM 3
ITEM 4
ITEM 5
ITEM 6
but i looking for some like:
<ítem value=1>ITEM 1
<ítem value=2>ITEM 2
<ítem value=3>ITEM 3
<ítem value=4>ITEM 4
<ítem value=5>ITEM 5
<ítem value=6>ITEM 6
then Access by SELECTED ITEM like
spinner.SelectedItemPosition.ToString();
or
spinner.SelectedItemId
something like that
Sam,27, im new in Android and C#
Thanks for your time.
8:09AM
Hi Sam.27. thanks for you response i appreciate it.
But im lookin for an example that include KEY/VALUE like DisplayMember/ValueMember in Winforms
IN Xmarin.Android i being use this to populate, but it only contains membersValue
<ítem> ITEM 1<ítem>
<ítem>ITEM 2<ítem>
<ítem> ITEM 3<ítem>
<ítem> ITEM 4<ítem>
<ítem>ITEM 5<ítem>
<ítem> ITEM 6<ítem>
but i looking for some like:
<ítem value=1>ITEM 1<ítem>
<ítem value=2>ITEM 2<ítem>
<ítem value=3>ITEM 3<ítem>
<ítem value=4>ITEM 4<ítem>
<ítem value=5>ITEM 5<ítem>
<ítem value=6>ITEM 6<ítem>
then Access by SELECTED ITEM like
spinner.SelectedItemPosition.ToString();
or
spinner.SelectedItemId
something like that
Sam,27, im new in Android and C#
Thanks for your time.
Hello @DimChris,
Key/value means: if you have a class like
and you want to populate a ComboBox in Windows Forms with a
List<Gender>
you can set the DataSource property with your genders and set the DisplayMember for showing GenderName in the dropdown and ValueMember for GenderId. So the ComboBox will show a gender, but when you select an item, you will get the id. This using Reflection, i think. This is one component (control/class) in Windows Forms, but in Xamarin the Spinner is only a view, a GUI element that show the elements and the adapter will handle the data. In this case @RenegadoMVA, you should implement an advanced adapter that will operate like this. It will have a DisplayMember, a ValueMember and a DataSource property. You can set the data source via this property instead in the constructor as third parameter. After setting the properties you have to use Reflection and get the definied values. You will provide the DisplayMember for the Spinner and the ValueMember for the selection.But i think this is too much effort. I would use Sam's solution instead. In the constructor i can set the "DisplayMember" by using a Linq Select() on the initial dataset and in the event handler from the position i can get the item.
I'm looking now for the same thing. But i think that you can solve this by using tag in your spinner. I'm in front for making the same thing you ask. When i will solve this i will post you my solution
DELETED
Ok i think i found an easy example for doing this.
XML FILE:
So the only thing you have now to do is to fill each aray with your sqlite values.Example inside string[] //your keys and inside int[] your values
Here is my example from sqlite to spinner key/value
https://github.com/dimis1989/Spinner_Key-Value
Hi @Cortez sorry to answer to late, but i was on vacation haha . but im back.
Well, as you say, is very hard to made this "simple" but i gonna try with all the tips and examples that you can suggested me.
I don´t know why Xamarin ( Microsoft) not keep the same way to DropDownList and Spinner.
but i need more knowledgement to see if this possible
Thank you!
Hi again @DimChris sorry for answer you to late. but i was on my vacations hahaha.
Excellent way to solve this @DimChris i gonna implement this in my project. so last question to you.
To get the Val1 array value when i select the value of the textView, which is the best way to do this: when i click in a button, get the Value of var VAL1
Best regards.
If i am right, you want when you clicking the button to take the value from spinner?
You can use the same method with key example: (name instead id)
Or another method which i prefer,
This one worked for me too :-) thanks