Xamarin ListView 显示项目来自 SQLite 数据库 C# Android [英] Xamarin ListView display item from SQLite Database C# Android

查看:32
本文介绍了Xamarin ListView 显示项目来自 SQLite 数据库 C# Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我而言,我想显示我创建的本地 SQLite 数据库中的项目,如下所示:

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);
            }

代码运行时 NO ERROR 但它只显示表格中的最后一项.这让我很困惑,所以我想问一下如何从特定表中检索所有数据?或者如果有人问过同样的问题,请分享链接.很多人欣赏.

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.

推荐答案

var alltables = tablelistout.Table<TableInfo>();
var data = new List<string>();
foreach (var listing in alltables)
{
     data.Add(listing.tname + "   -   " + listing.status);
}

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

我所做的只是将两件事移出循环.首先,我移出了数组的初始化.其次,我把适配器的listView+赋值移了出来.

All I did was move 2 things out of the loop. First, I moved out the initialization of the array. Second, I moved out the listView + assignation of the adapter.

你的问题是在循环中,你总是覆盖你在上一次迭代中所做的一切(像你说的那样留下最后一项).

Your issue is that in the loop, you were always overriding everything you had done in the previous iteration (leaving you with the last item like you said).

此外,您应该注意,如果您计划拥有大量数据,那么创建自定义适配器对您来说很重要.ArrayAdapter 是一个原生 Android 类,然后由 C# Xamarin 对象包装,这意味着每行将同时拥有 C# 和 Java 对象.它增加了开销,因为两个垃圾收集器都有工作要做,并可能导致性能问题.除了快速原型设计之外,Xamarin 开发人员通常会避免使用它.

Also, You should take note that it will be important for you to create a custom adapter if you plan on having a decent amount of data. ArrayAdapter is a native Android class which is then wrapped by a C# Xamarin object, meaning you will have both a C# and Java object per row. It adds overhead as both garbage collectors will have work to do and can cause performance issues. Xamarin devs tend to generally avoid it with the exception of quick prototyping.

另一方面,我会使用 FindViewById(Int32) 而不是 FindViewById(Int32) 并进行转换.FindViewById(Resource.Id.listtable) 在你的情况下.只是利用泛型的力量.

On another note, I would use the FindViewById<T>(Int32) instead of the FindViewById(Int32) and casting it. FindViewById<ListView>(Resource.Id.listtable) in your case. Just taking advantage of the power of generics.

这篇关于Xamarin ListView 显示项目来自 SQLite 数据库 C# Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆