下面是我的代码,在机子上面运行的时候总是会出错
Activity1.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;
using Android.Database;
using Android.Database.Sqlite;
namespace MonoAndroidApplication3
{
    [Activity(Label = "MonoAndroidApplication3", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int id;
        ListView lvInfo;
        EditText txtInfo;
        MySQLiteHelper db;
        ICursor cur;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.MainWindow);
            Button addInfo = FindViewById<Button>(Resource.Id.addInfo);
            Button changeInfo = FindViewById<Button>(Resource.Id.changeInfo);
            Button DelInfo = FindViewById<Button>(Resource.Id.DelInfo);
            txtInfo = FindViewById<EditText>(Resource.Id.txtInfo);
            db = new MySQLiteHelper(this);
            try
            {
                lvInfo = FindViewById<ListView>(Resource.Id.lvInfo);
                txtInfo = FindViewById<EditText>(Resource.Id.tvInfo);
                cur = db.Select();
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, Resource.Layout.listitem, cur, new string[] { "message" }, new int[] { Resource.Id.txtView });
                lvInfo.Adapter = adapter;
                lvInfo.ItemClick += (sender, e) =>
                    {
                        cur.MoveToPosition(e.Position);
                        id = cur.GetInt(0);
                        txtInfo.Text = cur.GetString(1);
                    };
                lvInfo.ItemSelected += (sender, e) =>
                    {
                        SQLiteCursor sc = (sender as AdapterView).SelectedItem as SQLiteCursor;
                        id = sc.GetInt(0);
                        txtInfo.Text = sc.GetString(1);  
                    };
           }
            catch (System.Exception ex)
            {
            }
            addInfo.Click += (sender,e) =>
            {
                if (string.IsNullOrEmpty(txtInfo.Text))  
                    return;
                db.Insert(txtInfo.Text);
                Init();
            };
            changeInfo.Click += (sender, e) =>
                {
                    if (string.IsNullOrEmpty(txtInfo.Text) || id == 0)
                        return;
                    try
                    {
                        db.Update(id, txtInfo.Text);
                        Init();
                    }
                    catch (System.Exception ex)
                    {
                    }
                };
            DelInfo.Click += (sender, e) =>
                {
                    if (id == 0)
                        return;
                    db.Delete(id);
                    Init();
                };
        }        private void Init()
        {
            try
            {
                cur.Requery();//就是在这里出的错,这句代码运行不了,这是为啥捏?
                lvInfo.InvalidateViews();
                txtInfo.Text = "";
                id = 0;
            }
            catch (SystemException ex)
            {
                txtInfo.Text = "error 1";
            }
        }
    }
}
mono 貌似网上资料很少,求会的大哥大姐交下,明天就要检查了
还有一个类:MySQLiteHelper.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 MonoAndroidApplication3
{
    public class MySQLiteHelper : Android.Database.Sqlite.SQLiteOpenHelper
    {
        //调用基类的构造函数,数据库名字为Data
        public MySQLiteHelper(Context context)
            : base(context, "Data", null, 1)
        { }
        //在OnCreate中新建数据库表Memo,有num字段及message字段
        public override void OnCreate(Android.Database.Sqlite.SQLiteDatabase db)
        {
            string sql = "create table Info(num integer primary key autoincrement,message text)";
            db.ExecSQL(sql);
        }
        //更新数据库时先将Memo删除再重新创建   
        public override void OnUpgrade(Android.Database.Sqlite.SQLiteDatabase db, int oldVersion, int newVersion)
        {
            string sql = "drop table if exists Info";
            db.ExecSQL(sql);
            OnCreate(db);
        }
        //查出Info表中的数据   
        public Android.Database.ICursor Select()
        {
            return this.ReadableDatabase.Query("Info", null, null, null, null, null, null);
        }
        //向Info中插入一条新数据   
        public long Insert(string newmessage)
        {
            ContentValues values = new ContentValues();
            values.Put("message", newmessage);
            return this.ReadableDatabase.Insert("Info", null, values);
        }
        //删除Info中一条记录   
        public void Delete(int num)
        {
            this.ReadableDatabase.Delete("Info", "num=?", new string[] { num.ToString() });
        }
        //根据num修改Info的记录   
        public void Update(int num, string info)
        {
            ContentValues values = new ContentValues();
            values.Put("message ", info);
            this.ReadableDatabase.Update("Info", values, "num=?", new string[] { num.ToString() });
        }
    }
}
布局的代码我就不上传了 - -

解决方案 »

  1.   

    是private void Init()
      {
      try
      {
      cur.Requery();//就是在这里出的错,这句代码运行不了,这是为啥捏?
      lvInfo.InvalidateViews();
      txtInfo.Text = "";
      id = 0;
      }
      catch (SystemException ex)
      {
      txtInfo.Text = "error 1";
      }
      }
    这里出了问题,没有报错,但是就是会运行catch里面的语句,把那句注释掉就不会转到catch执行了,为什么呢?是不是我的数据库操作有问题?还是其他地方出了问题?