首页 > 移动平台 > 详细

xamarin.android listview绑定数据及点击事件

时间:2015-03-12 20:41:27      阅读:3464      评论:1      收藏:0      [点我收藏+]

前言

  listview是用来显示数据列表的一个控件,今天给大家带来如何使用cursor进行数据绑定以及点击事件。

导读

   1.如何创建一个listview

 2.如何使用cursor进行绑定数据

 3.listview的点击事件

正文

  1.如何创建一个listview

   这里我们自定义一个listview的视图,首先打开Main.axml,拖一个listview放进去。

 右击Layout新建一个视图,名为UserListItemLayout.axml,拖两个textview进去,如图

技术分享

 这样我们就完成了一个自定义的listview,

 是用listview需要继承listactivity类,也可以不继承,不继承也有不继承的方法,我会把两个方法都写出来。

        SQLite vdb;
        ICursor cursor;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            //SetContentView(Resource.Layout.Main);
            ActionBar.SetDisplayHomeAsUpEnabled(true); 
            vdb = new SQLite(this);      
            cursor = vdb.ReadableDatabase.RawQuery("SELECT * FROM TestTable", null);
            StartManagingCursor(cursor);
            string[]  name = new string[]{ "name", "phone" };
            int[] phone = new int[] { Resource.Id.textName, Resource.Id.textPhone };
            ListAdapter = new SimpleCursorAdapter(this, Resource.Layout.UserListItemLayout, cursor,
             name,phone);           
        }

 这里我们给cursor绑定了数据,如何创建数据库请参考YZF的Xamarin.Android之SQLiteOpenHelper这里继承了listactivity,所以无法使用setcontentview。

 如何需要使用setcontentview的话,我们可以不继承listactivity,只需要把代码改成这样既可

 public class Activity1 : Activity
    {
        SQLite vdb;
        ICursor cursor;
        ListView listView;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            //SetContentView(Resource.Layout.Main);
            ActionBar.SetDisplayHomeAsUpEnabled(true); 
            listView = FindViewById<ListView>(Resource.Id.listView1);
            vdb = new SQLite(this);      
            cursor = vdb.ReadableDatabase.RawQuery("SELECT * FROM TestTable", null);
            StartManagingCursor(cursor);
            string[]  name = new string[]{ "name", "phone" };
            int[] phone = new int[] { Resource.Id.textName, Resource.Id.textPhone };
            listview.Adapter = new SimpleCursorAdapter(this, Resource.Layout.UserListItemLayout, cursor,
             name,phone);            
        }

 效果图如下

技术分享

 3.listview的点击事件

   这里listview的点击事件有两种方法,第一种是继承listactivity使用的方法,第二种是不继承listactivity的使用方法。

  这里我们可以直接重写OnListItemClick方法既可调用listview的点击事件

        protected override void OnListItemClick(ListView l, View v, int position, long id)
        {

        }

 或者你使用了第二种方法

        this.listView.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs>(ListView_ItemClick);
        void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
           
        }

 最后效果图如下

技术分享

技术分享

 

 

xamarin.android listview绑定数据及点击事件

原文:http://www.cnblogs.com/lihuazou/p/4333366.html

(4)
(3)
   
举报
评论 一句话评论(1
2015-12-30 21:28:14
SQLite是怎么回事?无法使用。
回复
 (9)
 (3)
1条  
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!