vendredi 4 décembre 2015

Xamarin ListView display item from SQLite Database C# Android

in my case i wanted to display items from local SQLite database which i created as shown below:

public string CreateDB() //create database
    {
        var output = "";
        string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "IsoModule.db3");
        output = "Database Created";
        return output;
    }

    public string CreateTable() //create table
    {
        try
        {
            string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "IsoModule.db3");
            var db = new SQLiteConnection(dbPath);
            db.CreateTable<UserInfo>();
            db.CreateTable<TableInfo>();
            string result = "Table(s) created";
            return result;
        }
        catch (Exception ex)
        {
            return ("Error" + ex.Message);
        }
    }

and this is my code where i wish to retrieve data

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "IsoModule.db3");
        var tablelistout = new SQLiteConnection(path);
        var alltables = tablelistout.Table<TableInfo>();
        foreach (var listing in alltables)
        {
            var from = new string[]
            {
                listing.tname + "   -   " + listing.status 
            };

            ListView listtable = (ListView)FindViewById(Resource.Id.listtable);
            listtable.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, from);
            }

the code runs with NO ERROR but it only display last item in the table. it is confusing me, so i would like to ask how can i retrieve all the data from specific table? or if someone has asked the same question please share me the link. many appreciate.

Aucun commentaire:

Enregistrer un commentaire