[英]How do I get the id from a XAML ListView when more than one table is used?
I am populating a ListView
with SQLite data from two or more tables. 我用来自两个或多个表的SQLite数据填充ListView
。 When selecting from table I wish to get the ID of the selected item but lv.SelectedItem
is returning a null
value. 从表中选择时,我希望获取所选项目的ID,但是lv.SelectedItem
返回null
值。 No trouble with single tables. 单个表没有问题。
I have searched everywhere and most people are just having problems joining the tables and haven't got as far as selecting. 我到处搜索过,大多数人在加入表格时都遇到问题,还没有选择的余地。
public int ShoppingListID
{
get { return listID; }
set { listID = value;}
}
public int ShopID
{
get { return shopID; }
set { shopID = value; }
}
public int ShopID
{
get { return shopID; }
set { shopID = value; }
}
public string ShopName
{
get { return shopName; }
set { shopName = value; }
}
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
var allList = (from s in conn.Table<ShoppingLists>() join p in
conn.Table<Shops>() on s.ShopID equals
p.ShopID select new
{
s.ShoppingListID,
p.ShopName
}).ToList();
lvShoppingLists.ItemsSource = allList;
}
<ListView x:Name="lvShoppingLists"
ItemSelected="LvShoppingLists_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Grid.Column="0"
TextColor="Black"
Text="{Binding ShoppingListID}" />
<Label Grid.Column="1"
TextColor="Black"
Text="{Binding ShopName}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
private void LvShoppingLists_ItemSelected(object sender,
SelectedItemChangedEventArgs e)
{
var selectedList = lvShoppingLists.SelectedItem as ShoppingLists;
GlobalVariables.SelectedShoppingListID = selectedList.ShoppingListID;
Navigation.PushAsync(new ShoppingListPage());
}
Everything compiles and runs fine, but upon selecting an item from the ListView
on the mainpage var selectedList
returns a null
. 一切都可以编译并正常运行,但是从ListView
上的ListView
选择一项时, var selectedList
返回null
。 Debug shows that the information ( id
and Name
) is being carried to the selected item but not being converted. 调试显示信息( id
和Name
)被携带到所选项目,但没有被转换。 The null
value means that the variable is unable to be used in the query for the new page and I get a database error. null
值表示该变量无法在新页面的查询中使用,并且出现数据库错误。 I suspect that it is to do with the fact that the the class Shopping list has two int entries and the name is causing the problem. 我怀疑这与“购物”类具有两个int条目且名称引起问题这一事实有关。
Answer turns out to be:
Use a new class AllLists
public int ShoppingListID
{
get { return listID; }
set { listID = value; }
}
public string ShopName
{
get { return shopName; }
set { shopName = value; }
}
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
var allList = (from s in conn.Table<ShoppingLists>() join p in
conn.Table<Shops>() on s.ShopID equals
p.ShopID select new **AllLists**
{
ShoppingListID = s.ShoppingListID,
ShopName = p.ShopName
});
lvShoppingLists.ItemsSource = allList;
}
The selected item ID can then be
var selectedList = lvShoppingLists.SelectedItem as AllLists;
GlobalVariables.SelectedShoppingListID = selectedList.ShoppingListID;
Navigation.PushAsync(new ShoppingListPage());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.