• 第三课 Spinner的使用


    Spinner的表现形式相当于C#的ComboBox,样子如下图:

    image

    但选择项的添加方式相当不一样,必须使用数据适配器,上例子。

    Layout——Main.axml

    <?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">
        <TextView
            android:text="Text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView1" />
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinner1" />
    </LinearLayout>

    Activity1.cs

    using System;
     
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    using Android.Database;
    using Android.Provider;
     
    namespace SpinnerSample
    {
        [Activity(Label = "SpinnerSample", MainLauncher = true, Icon = "@drawable/icon")]
        public class SpinnerSample : Activity
        {
            private ICursor _BookmarkCursor;
            public ICursor BookmarkCursor
            {
                get
                {
                    if (_BookmarkCursor == null)
                    {
                        _BookmarkCursor = GetBookmarkCursor();
                    }
                    return _BookmarkCursor;
                }
                set { _BookmarkCursor = value; }
            }
     
            public ICursor GetBookmarkCursor()
            {
                return ManagedQuery(Browser.BookmarksUri,
                    new string[] { Browser.BookmarkColumns.Title, Browser.BookmarkColumns.Url, Browser.BookmarkColumns.InterfaceConsts.Id },
                    null,
                    null,
                    null);
            }
     
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
     
                SetContentView(Resource.Layout.Main);
     
                CreateSpinner();
            }
            
            int LastSpinnerSelectedPosition;
            public void CreateSpinner()
            {
                LastSpinnerSelectedPosition = 0;
                var TargetSpinner = FindViewById<Spinner>(Resource.Id.spinner1);
                SimpleCursorAdapter SpinnerAdapter = new SimpleCursorAdapter(this,
                    Android.Resource.Layout.SimpleSpinnerItem, BookmarkCursor,
                    new string[] { Browser.BookmarkColumns.Title },
                    new int[] { Android.Resource.Id.Text1 });
                SpinnerAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
                TargetSpinner.Adapter = SpinnerAdapter;
                TargetSpinner.Prompt = "Select ...";
     
                TargetSpinner.ItemSelected += TargetSpinner_ItemSelected;
     
            }
     
            void TargetSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
            {
                var CurSpinner = (Spinner)sender;
                var CurSelectedIndex = CurSpinner.SelectedItemPosition;
                if (LastSpinnerSelectedPosition != CurSelectedIndex)
                {
                    ICursor SelectedItem = (ICursor)CurSpinner.SelectedItem;
                    var UrlColumnIndex = SelectedItem.GetColumnIndex(Browser.BookmarkColumns.Url);
                    TextView tv = FindViewById<TextView>(Resource.Id.textView1);
                    tv.Text = SelectedItem.GetString(UrlColumnIndex);
                    LastSpinnerSelectedPosition = CurSelectedIndex;
                }
            }
        }
    }
     

    运行此程序需要READ_HISTORY_BOOKMARKS权限,设置方式如下图。

    image

  • 相关阅读:
    Windows Server 2008安装Memcached笔记
    解决powerDesinger12逆向工程报错:Unable to list the tables
    冒泡排序算法
    ASP.NET面试题(推荐_有答案)
    ASP.NET服务器控件分类简介
    将excel文件中的数据导入导出至SQL数据库中(Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0|office2003和office2007)
    关于sql access excel以及在web.config中数据库连接字符串的写法
    ODBC / OLEDB___DAO / RDO / ADO
    什么是CSV格式文档
    AppSettings和ConnectionStrings的区别
  • 原文地址:https://www.cnblogs.com/catzhou/p/3591208.html
Copyright © 2020-2023  润新知