Xamarin SimplerCursorAdapter 适配器(三)

        SimplerCursorAdapter的使用步骤:
            1、根据需要定义ListView每行所实现的布局。
            2、实现数据表的查询操作,获取ICursor对象。
            3、根据SimplerCursorAdapter对象。
            4、将ListView绑定到SimplerCursorAdapter上。

 

信息列表的布局: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="ID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtID" />
    <TextView
        android:text="NAME"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtName" />
    <TextView
        android:text="TEL"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtTel" />
</LinearLayout>

 

 

逻辑代码:MainActivity.cs

 

using System;

using Android.App;

using Android.Content;

using Android.Runtime;

using Android.Views;

using Android.Widget;

using Android.OS;

using Android.Database.Sqlite;

using Android.Database;

namespace App1

{    

  [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]    

  public class MainActivity : ListActivity   

   {        

       int count = 1;

            ICursor cursor = null;

          //SimplerCursorAdapter的使用步骤:

                 1、根据需要定义ListView每行所实现的布局。

                 2、实现数据表的查询操作,获取ICursor对象。

                 3、根据SimplerCursorAdapter对象。

                 4、将ListView绑定到SimplerCursorAdapter上。

        protected override void OnCreate(Bundle bundle)

        {

            base.OnCreate(bundle);

            LocalSqliteHelper sqlLocalSqliteHelper = new LocalSqliteHelper(this);

            cursor=sqlLocalSqliteHelper.WritableDatabase.RawQuery("select _id,name,tel from LinkManInfo",null);

            StartManagingCursor(cursor);

            this.ListAdapter=new SimpleCursorAdapter(this,Resource.Layout.Main,cursor,new string[] { "_id","name","tel" },new int[] { Resource.Id.txtID,Resource.Id.txtName,Resource.Id.txtTel });

            // Set our view from the "main" layout resource  

            //SetContentView(Resource.Layout.Main);  

            //选项单击事件

            this.ListView.ItemClick += (sender, e) =>

            {

                cursor.MoveToPosition(e.Position);

                string[] values = { cursor.GetString(1), cursor.GetString(2) };

                Intent intent = new Intent(this, typeof(ShowLinkMan));

                intent.PutExtra("linkMan", values);

                StartActivity(intent);

            };

        }

        protected override void OnDestroy()

        {

            base.OnDestroy();

            StopManagingCursor(cursor);

            cursor.Close();

        }

    }

}

 

 

 

接收信息的页面布局:ShowLinkMan.axml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textName" />
    <TextView
        android:text="TEL"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textTel" />
</LinearLayout>

 

代码逻辑:ShowLinkMan.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Android.App;

using Android.Content;

using Android.OS;

using Android.Runtime;

using Android.Views;

using Android.Widget;

namespace App1

{   

  [Activity(Label = "ShowLinkMan")]

    public class ShowLinkMan : Activity

    {

        protected override void OnCreate(Bundle savedInstanceState)

        {

            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.ShowLinkMan);

            // Create your application here

            string[] values=Intent.GetStringArrayExtra("linkMan");

            FindViewById<TextView>(Resource.Id.textName).Text = "姓名:" + values[0];

            FindViewById<TextView>(Resource.Id.textTel).Text = "电话:" + values[1];

        }

    }

}

 

posted @ 2016-05-15 22:06  风中寻觅  阅读(199)  评论(0)    收藏  举报