如何从自己的sqlite文件填充listview?

时间:2016-09-28 16:35:55

标签: android sqlite listview xamarin android-sqlite

我正在使用xamarin,我目前正在使用自定义阵列适配器,但我的教授。告诉我,我们的系统应该包括数据库,所以我一直在搜索,直到找到json,但有人告诉我,如果我使用SQlite,我可以更快地检索我的数据。所以我做了..我已经使用SQLite Manager(firefox插件)创建了我自己的预先填充的SQLite数据库文件。我一直在关注这个link使用我自己的SQLite数据库文件..我不知道接下来要做什么...已经搜索了近2个小时但找不到任何有用的例子。对不起是一个菜鸟程序员。谁能给我有用的链接?

到目前为止:

public class DBOpenHelper:SQLiteOpenHelper
{
    private static string DB_PATH = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    private static string DB_NAME = "akyatpinas.db";
    private static int VERSION = 1;
    private Context context;

    public DBOpenHelper(Context context) : base(context, DB_NAME, null, VERSION)
    {
        this.context = context;
    }

    private string GetSQLiteDBPath()
    {
        return Path.Combine(DB_PATH, DB_NAME);
    }

    public override SQLiteDatabase WritableDatabase
    {
        get { return CreateSQLiteDB(); }
    }

    private SQLiteDatabase CreateSQLiteDB()
    {
        SQLiteDatabase sqliteDB =null;
        string path = GetSQLiteDBPath();
        Stream streamSQLite = null;
        FileStream streamWriter = null;
        Boolean isSQLiteInit = false;

        try
        {
            if (File.Exists(path))
                isSQLiteInit = true;
            else
            {
                streamSQLite = context.Resources.OpenRawResource(Resource.Raw.akyatpinas);
                streamWriter = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write);
                if (streamSQLite != null && streamWriter != null)
                {
                    if (CopySQLiteDB(streamSQLite, streamWriter))
                        isSQLiteInit = true;
                }
            }
            if (isSQLiteInit)
                sqliteDB = SQLiteDatabase.OpenDatabase(path, null, DatabaseOpenFlags.OpenReadonly);
        }
        catch
        {
            // ignored
        }
        return sqliteDB;
    }

    private bool CopySQLiteDB(Stream readStream, FileStream writeStream)
    {
        bool isSuccess = false;
        int length = 256;
        Byte[] buffer = new Byte[length];
        try
        {
            // write the required bytes
            int bytesRead = readStream.Read(buffer, 0, length);
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, length);
            }
            isSuccess = true;
        }
        catch
        {
            //ignore 
        }
        finally
        {
            readStream.Close();
            writeStream.Close();
        }
        return isSuccess;
    }


    public override void OnCreate(SQLiteDatabase db)
    {

    }

    public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:0)

因此,根据我的阅读,您创建了一个SQLite数据库。你有没有把它移到你的app目录?这应该是你迈出的第一步。如果还没有,请在src / main中创建一个assets目录并将数据库放在那里,所以完整路径应该是src / main / assets / yourdatabase.db

this link上的DatabaseHelper课程应告诉您与您的沟通方式。您需要确保将DATABASE_NAME常量更改为assets目录中文件的名称,因此对于上述内容,将为yourdatabase.db