我想在我的数据库中获取我的数据的项目密钥,但正如您在DataSnapshot函数中看到的那样,我试图获取项目密钥,但它返回空白。我正试图将所有项目键绑定到我的数据提取。所以每个取得的物品都应该有钥匙,所以我可以使用钥匙。但是,除了项目键以外,所有其他元素都被提取。为什么项目键不显示?感谢您的帮助如何从Firebase获取项目密钥
结构
型号
public Hero(string uid,string name, string achievment, string history, string quote)
{
this.uid = uid;
this.name = name;
this.achvievment = achievment;
this.history = history;
this.quote = quote;
}
public HashMap ToMap()
{
HashMap map = new HashMap();
map.Put("Uid", this.uid);
map.Put("Name", this.name);
map.Put("Achievement", this.achvievment);
map.Put("History", this.history);
map.Put("Quote", this.quote);
return map;
}
public string Uid
{
get { return uid; }
set { uid = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Image
{
get { return image; }
set { image = value; }
}
public string Achievement
{
get { return achvievment; }
set { achvievment = value; }
}
public string History
{
get { return history; }
set { history = value; }
}
public string Quote
{
get { return quote; }
set { quote = value; }
}
保存数据
//this is how i save my data which works fine
mHero.Push().SetValue(new PeaceHero(user.Uid,HeroName.Text, HeroAchv.Text, HeroHistory.Text, HeroQuote.Text).ToMap());
获取数据
public void OnDataChange(DataSnapshot snapshot)
{
var items = snapshot.Children?.ToEnumerable<DataSnapshot>();
HashMap map;
foreach (DataSnapshot item in items)
{
map = (HashMap)item.Value;
Hero.Add(new Hero(map.Get(item.Key)?.ToString(),map.Get("Name")?.ToString(), map.Get("Achievement")?.ToString(),map.Get("History")?.ToString(),map.Get("Quote")?.ToString()));
Console.WriteLine("Sending item key" +item.Key.ToString());
key = item.Key;
}
HeroAdapter adapter = new PeaceHeroAdapter(Hero,this);
Console.Write("The key is "+key);
}
问题是什么?需要调用'Key.ToString()'如果是这种情况,为什么键没有打印在'Console.WriteLine(“发送项目键”+ item.Key.ToString());' –
我试图绑定项目在foreach循环中关键每个数据调用 – Switz