当前位置:首页 > 数据库 > SQlite

Xamarin.Android 使用 SQLite 出现 Index -1 requested, with a size of 10 异常

异常: Android.Database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 10

技术分享图片

此错误是数据返回到ICursor无法确定获取列的索引,那么需要加上一下代码即可。

            if (i == 0)             //确定游标位置{
    ic.MoveToFirst();
}
else
{
    ic.MoveToNext();
}

完整代码Demo:

            ///
            <summary>
            ///
             查询数据

            ///
            </summary>
            void
             QueryData()
{
    ICursor ic =  Localhost_DataBase.Query("tb_person", null, null, null, null, null, null);
    for (int i = 0; i < ic.Count; i++)
    {
        if (i == 0)             //确定游标位置        {
            ic.MoveToFirst();
        }
        else
        {
            ic.MoveToNext();
        }

        person = new Person();
        person.Id = ic.GetString(ic.GetColumnIndex("Id"));
        person.Name = ic.GetString(ic.GetColumnIndex("name"));
        person.Age = ic.GetString(ic.GetColumnIndex("age"));
        person.Sex= ic.GetString(ic.GetColumnIndex("sex"));
        person.IdCard = ic.GetString(ic.GetColumnIndex("idcard")); 
        list.Add(person); 
    }
    lv_Person.Adapter = new ListViewAdapter(this, list);
}

 

原文:https://www.cnblogs.com/swjian/p/9360603.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!