• Xamarin.Android 记事本(一)


    导读

       1.视图及数据库的创建

       2.listview数据绑定

       3.listview的点击事件

    正文

      如何创建一个listview,大家可以看这里,基本流程操作是一模一样的,我就不多说了,然后就是新建一个数据库,代码如下

    class Sqlite : SQLiteOpenHelper
        {
            public Sqlite(Context context)
                : base(context, "notebooksql.db", null, 1)
            { }
    
            public override void OnCreate(SQLiteDatabase db)
            {
                db.ExecSQL("CREATE TABLE NoteBooksql ( _id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,title TEXT NOT NULL,context TEXT NOT NULL,time TIME NOT NULL)");
                db.ExecSQL("INSERT INTO NoteBooksql (title,context,time)values('这是第一篇笔记','笔记笔记第一篇笔记笔记笔记第一篇笔记笔记笔记第一篇笔记笔记笔记第一篇笔记','2015-3-15')");
                db.ExecSQL("INSERT INTO NoteBooksql (title,context,time)values('这是第二篇笔记','笔记笔记第二篇笔记笔记笔记第二篇笔记笔记笔记第二篇笔记笔记笔记第二篇笔记','2015-3-15')");
            }
    
            public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
            {
                db.ExecSQL("DROP TABLE IF EXISTS NoteBooksql");
                OnCreate(db);
            }
        }

    这里设置了一个自增主键和三个字段,然后我添加了两条默认数据。

    数据库创建完成之后我们打开Activity1,继承listactivity,给listview进行绑定数据

     protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                vdb = new Sqlite(this);
                cursor = vdb.ReadableDatabase.RawQuery("SELECT * FROM NoteBooksql", null);
                StartManagingCursor(cursor);
                string[] title = new string[] { "title", "time" };
                int[] time = new int[] { Resource.Id.textView1, Resource.Id.textView2 };
                ListAdapter = new SimpleCursorAdapter(this, Resource.Layout.Item, cursor,
                 title, time);
            }

    效果图如上

    有了数据的listview却不能操作那边是毫无作用,我们可以重新OnListItemClick方法给listview添加点击事件

    protected override void OnListItemClick(ListView l, View v, int position, long id)
            {
                string title= v.FindViewById<TextView>(Resource.Id.textView1).Text.ToString();
                string time = v.FindViewById<TextView>(Resource.Id.textView2).Text.ToString();
                vdb = new Sqlite(this);
                cursor = vdb.ReadableDatabase.RawQuery("SELECT * FROM NoteBooksql where title= '" + title+ "' and time = '" + time + "'", null);
                cursor.MoveToFirst();
                string Nid = cursor.GetString(cursor.GetColumnIndex("_id"));
                var intent = new Intent(this, typeof(Note));
                intent.PutExtra("id", Nid);
                StartActivity(intent);
                this.Finish();
            }

    这里为了图方便,我直接根据title和time获取id。

    效果图如下

  • 相关阅读:
    (一)Kubernetes 系统基础
    Linux-网络RAID技术DRBD
    Linux-存储服务之NFS
    ELK快速入门(五)配置nginx代理kibana
    ELK快速入门(四)filebeat替代logstash收集日志
    ELK快速入门(三)logstash收集日志写入redis
    渗透测试工具集合
    CVE-2019-0708(非蓝屏poc)远程桌面代码执行漏洞复现
    代码审计-thinkphp3.2.3框架漏洞sql注入
    渗透测试-端口复用正向后门
  • 原文地址:https://www.cnblogs.com/lihuazou/p/4335393.html
Copyright © 2020-2023  润新知