Hi,

A nice simple solution is this

using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace spinner
{
        [Activity (Label = "spinner", MainLauncher = true)]
        public class Activity1 : Activity
        {
                protected override void OnCreate (Bundle bundle)
                {
                        base.OnCreate (bundle);

                        // Set our view from the "main" layout resource
                        SetContentView (Resource.Layout.Main);
                        TextView courseResult = FindViewById<TextView> 
(Resource.Id.textCourse);
                        Spinner courseTitle = FindViewById<Spinner> 
(Resource.Id.spinner1);
                        List<string> courses = new List<string> ();
courses.AddRange (new string[]{"Chemistry", "Physics", "Biology", "Maths", "German"});
                        List<int> courseNumbers = new List<int> ();
                        courseNumbers.AddRange (new int[] {101, 202, 303, 404, 
505});
ArrayAdapter<string> adapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerDropDownItem);
                        foreach (string s in courses)
                                adapter.Add (s);
                        courseTitle.Adapter = adapter;

courseTitle.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
                        {
                                courseResult.Text = courseNumbers 
[e.Position].ToString ();
                        };
                }
        }
}

with the view being

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:gravity="center_horizontal">
        <TextView
            android:text="Course title"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/textCourseName"
            android:layout_marginRight="12.7dp" />
        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/spinner1" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2"
        android:layout_marginTop="12dp"
        android:gravity="center_horizontal">
        <TextView
            android:text="Course number"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/textView1"
            android:layout_marginRight="8.0dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/textCourse" />
    </LinearLayout>
</LinearLayout>

This code won't save the spinner values or anything like that, it's just a quick demo to show you how it's done.

Can I suggest that you read the recipes on the Xamarin website as well? There are a lot of demos on there that would help you with the problems you're seeing.

Remember, unless it's android specific, bog standard C# works. Even if it is android specific, you can still use bog standard C# in the code. For example, in my quick and simple solution, I could have used a dictionary(int, string). This associates an int with a string and is rather a neat way to code, however, it's overkill here.

Paul
--
"Space," it says, "is big. Really big. You just won't believe how vastly, hugely, mindbogglingly big it is. I mean, you may think it's a long way down the road to the chemist's, but that's just peanuts to space, listen..."
Hitch Hikers Guide to the Galaxy, a truly remarkable book!

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to