我想从 SQLite DB 获取所有信息
I want to take all the information from SQLite DB
我正在尝试从 Xamarin Forms
中的 SQLite
文件中获取 iOS
的所有信息,并将其放入单个 cell
中的 Label
在 TableView
。我正在尝试以下操作:
public partial class ErrorCell : UITableViewCell
{
public ErrorCell (IntPtr handle) : base (handle)
{
}
internal void UpdateCell(Error error)
{
errorDescription.Text = error.Description;
}
}
class TableSource : UITableViewSource
{
List<Error> errors;
public TableSource(List<Error> errors)
{
this.errors = errors;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = (ErrorCell)tableView.DequeueReusableCell("cell_Id", indexPath);
var errorid = errors[indexPath.Row];
cell.UpdateCell(errorid);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return errors.Count;
}
}
当我尝试从数据库调用获取所有项目的方法时
public class ErrorDataBase
{
readonly SQLiteAsyncConnection database;
public ErrorDataBase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<Error>().Wait();
}
public Task<List<Error>> GetItemsAsync()
{
return database.Table<Error>().ToListAsync();
}
}
在这里:
public partial class ViewController : UIViewController
{
static ErrorDataBase database;
public ViewController(IntPtr handle) : base(handle)
{
}
List<Error> errors;
public override void ViewDidLoad()
{
base.ViewDidLoad();
errors = new List<Error>
{
new Error()
{
Description="hufhdsuhufds"
}
,
new Error()
{
Description="Robot died and now we have to go and buy another robot"
}
,
new Error()
{
Description="Dead Robot revived!"
}
};
errorListView.Source = new TableSource(errors);
//errorListView.Source = await Database.GetItemsAsync();
errorListView.RowHeight = UITableView.AutomaticDimension;
errorListView.EstimatedRowHeight = 90f;
errorListView.ReloadData();
}
public static ErrorDataBase Database
{
get
{
if (database == null)
{
database = new ErrorDataBase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SQLiteDB.db"));
}
return database;
}
}
}
errorListView.Source = await Database.GetItemsAsync();
我收到以下错误:
Cannot convert from SystemCollectionsGeneric.List to
UITableViewSource
以我的开发经验,实在是解决不了,请问有没有人能帮帮我?
您正在将 List
分配给 UITableViewSource
,类型不匹配。
修改为
var list = await Database.GetItemsAsync();
errorListView.Source = new TableSource(list);
我发现,如果我想从 table
中获取数据,我必须先创建 table
,而我正尝试使用完整的 database
来做到这一点,只是提取数据,但我还没有找到解决方案。
我正在尝试从 Xamarin Forms
中的 SQLite
文件中获取 iOS
的所有信息,并将其放入单个 cell
中的 Label
在 TableView
。我正在尝试以下操作:
public partial class ErrorCell : UITableViewCell
{
public ErrorCell (IntPtr handle) : base (handle)
{
}
internal void UpdateCell(Error error)
{
errorDescription.Text = error.Description;
}
}
class TableSource : UITableViewSource
{
List<Error> errors;
public TableSource(List<Error> errors)
{
this.errors = errors;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = (ErrorCell)tableView.DequeueReusableCell("cell_Id", indexPath);
var errorid = errors[indexPath.Row];
cell.UpdateCell(errorid);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return errors.Count;
}
}
当我尝试从数据库调用获取所有项目的方法时
public class ErrorDataBase
{
readonly SQLiteAsyncConnection database;
public ErrorDataBase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<Error>().Wait();
}
public Task<List<Error>> GetItemsAsync()
{
return database.Table<Error>().ToListAsync();
}
}
在这里:
public partial class ViewController : UIViewController
{
static ErrorDataBase database;
public ViewController(IntPtr handle) : base(handle)
{
}
List<Error> errors;
public override void ViewDidLoad()
{
base.ViewDidLoad();
errors = new List<Error>
{
new Error()
{
Description="hufhdsuhufds"
}
,
new Error()
{
Description="Robot died and now we have to go and buy another robot"
}
,
new Error()
{
Description="Dead Robot revived!"
}
};
errorListView.Source = new TableSource(errors);
//errorListView.Source = await Database.GetItemsAsync();
errorListView.RowHeight = UITableView.AutomaticDimension;
errorListView.EstimatedRowHeight = 90f;
errorListView.ReloadData();
}
public static ErrorDataBase Database
{
get
{
if (database == null)
{
database = new ErrorDataBase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SQLiteDB.db"));
}
return database;
}
}
}
errorListView.Source = await Database.GetItemsAsync();
我收到以下错误:
Cannot convert from SystemCollectionsGeneric.List to UITableViewSource
以我的开发经验,实在是解决不了,请问有没有人能帮帮我?
您正在将 List
分配给 UITableViewSource
,类型不匹配。
修改为
var list = await Database.GetItemsAsync();
errorListView.Source = new TableSource(list);
我发现,如果我想从 table
中获取数据,我必须先创建 table
,而我正尝试使用完整的 database
来做到这一点,只是提取数据,但我还没有找到解决方案。