![](/img/trans.png)
[英]ListView items not completely loading from SQLite database [Xamarin.Forms]
[英]Items in database sqlite are not updating (Xamarin.Forms)
我在 Visual Studio 中使用 Xamarin.Forms 並且我有一個我在表格中插入的產品列表。 當我嘗試更新 Cantidad、Precio 或其他功能時,Articulo 不會更新,並且列表會像以前一樣顯示。 我正在嘗試將 PK 用作字符串,因此當我嘗試更新項目時,我需要 Articulo 的 Producto,它會驗證 Producto 的 Id 是否存在於數據庫中,然后進行更新。
這是我的 Model Articulo.cs
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
namespace Saansa.Modelos
{
public class Articulo
{
[PrimaryKey,AutoIncrement]
public string Id { get; set; }
public string Producto { get; set; }
public int Precio { get; set; }
public int Cantidad { get; set; }
public string MasterCategory { get; set; }
public string Category1 { get; set; }
public string Category2 { get; set; }
public string Category3 { get; set; }
}
}
這是我的 xalm 文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Saansa"
x:Class="Saansa.Inventario"
Title="Inventario">
<StackLayout BackgroundColor="#f5cda2">
<Image Margin="0,0,0,0" HeightRequest="10" Source="inventario.png" ></Image>
<Label Margin="0,0,0,0" Text="Mi Cafe Delicias" FontAttributes="Bold"
FontSize="Large" TextColor="#b27b4b" HorizontalTextAlignment="Center" ></Label>
<Entry x:Name="txtProducto" Placeholder="Código Producto"></Entry>
<Entry x:Name="txtNombre" Placeholder="Nombre del producto"></Entry>
<Entry x:Name="txtPrecio" Placeholder="Precio del producto"></Entry>
<Entry x:Name="txtCantidad" Placeholder="Cantidad del Producto"></Entry>
<Entry x:Name="txtMainCategory" Placeholder="Categoria general"></Entry>
<Entry x:Name="txtSub1" Placeholder="Subcategoria 1"></Entry>
<Entry x:Name="txtSub2" Placeholder="Subcategoria 2"></Entry>
<Entry x:Name="txtSub3" Placeholder="Subcategoria 3"></Entry>
<StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
<Button x:Name="btnAdd" WidthRequest="200" Text="Añadir" Clicked="BtnAdd_Clicked"
BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
<Button x:Name="btnRead" WidthRequest="200" Text="Buscar" Clicked="BtnRead_Clicked"
BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
</StackLayout>
<StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
<Button x:Name="btnUpdate" WidthRequest="200" Text="Actualizar" Clicked="BtnUpdate_Clicked"
BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
<Button x:Name="btnDelete" WidthRequest="200" Text="Borrar" Clicked="BtnDelete_Clicked"
BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
</StackLayout>
<ListView x:Name="lstArticulo" BackgroundColor="#f5cda2">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Producto}"/>
<Label Text="{Binding Id}" HorizontalOptions="EndAndExpand"
TextColor="Black"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
在我的 Inventario.xalm.cs 我的更新選項是這樣的:
private async void BtnUpdate_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtProducto.Text))
{
Modelos.Articulo articulo = new Modelos.Articulo()
{
Id = txtProducto.Text,
Producto = txtNombre.Text,
Precio = Convert.ToInt32(txtPrecio.Text),
Cantidad = Convert.ToInt32(txtCantidad.Text),
MasterCategory = txtMainCategory.Text,
Category1 = txtSub1.Text,
Category2 = txtSub2.Text,
Category3 = txtSub3.Text
};
//Update Person
await App.SQLiteDb.SaveItemAsync(articulo);
txtNombre.Text = string.Empty;
txtCantidad.Text = string.Empty;
txtProducto.Text = string.Empty;
txtPrecio.Text = string.Empty;
txtMainCategory.Text = string.Empty;
txtSub1.Text = string.Empty;
txtSub2.Text = string.Empty;
txtSub3.Text = string.Empty;
await DisplayAlert("Success", "Person Updated Successfully", "OK");
//Get All Persons
var articuloLista = await App.SQLiteDb.GetItemsAsync();
if (articuloLista != null)
{
lstArticulo.ItemsSource = articuloLista;
}
}
else
{
await DisplayAlert("Required", "Please Enter Nombre articulo", "OK");
}
}
和我的 SQLiteHekper.cs:
public Task<int> SaveItemAsync(Modelos.Articulo articulo)
{ //articulo.Cantidad. !string.IsNullOrEmpty(articulo.Cantidad)
if (!string.IsNullOrEmpty(articulo.Id))
{
//probando esto
//var nuevoArticulo = GetItemAsync(articulo.Producto);
return db.UpdateAsync(articulo);
}
else
{
return db.InsertAsync(articulo);
}
}
錯誤是它沒有得到 object,這就是為什么我們首先需要調用它然后編輯我們想要更新的 Articulo。
Private async void BtnUpdate_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtNombre.Text))
{
Modelos.Articulo articulo = await App.SQLiteDb.GetItemAsync(txtNombre.Text);
//Aparentemente dejar el texto vacío no es equivalente a "", por eso el +"0"
articulo.Cantidad = (string.Equals("0", txtCantidad.Text + "0")) ? articulo.Cantidad : Convert.ToInt32(txtCantidad.Text);
articulo.Category1 = (string.Equals("0", txtSub1.Text + "0")) ? articulo.Category1 : txtSub1.Text;
articulo.Category2 = (string.Equals("0", txtSub2.Text + "0")) ? articulo.Category2 : txtSub2.Text;
articulo.Category3 = (string.Equals("0", txtSub3.Text + "0")) ? articulo.Category3 : txtSub3.Text;
articulo.MasterCategory = (string.Equals("0", txtMainCategory.Text + "0")) ? articulo.MasterCategory : txtMainCategory.Text;
articulo.Precio = (string.Equals("0", txtPrecio.Text + "0")) ? articulo.Precio : Convert.ToInt32(txtPrecio.Text);
articulo.Costo = (String.Equals("0", txtCosto.Text + "0")) ? articulo.Costo : Convert.ToInt32(txtCosto.Text);
articulo.Producto = (string.Equals("0", txtProducto.Text + "0")) ? articulo.Producto : txtNombre.Text;
await App.SQLiteDb.SaveItemAsync(articulo);
txtNombre.Text = string.Empty;
txtCantidad.Text = string.Empty;
txtProducto.Text = string.Empty;
txtCosto.Text = string.Empty;
txtPrecio.Text = string.Empty;
txtMainCategory.Text = string.Empty;
txtSub1.Text = string.Empty;
txtSub2.Text = string.Empty;
txtSub3.Text = string.Empty;
await DisplayAlert("Success", "Producto actualizado con éxito", "OK");
}
else
{
await DisplayAlert("Required", "Please Enter código articulo", "OK");
}
}
這使得首先帶來object,檢查是否某些數據不在條目中以使其保持原樣,最后更新object。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.