我想在我的数据库中获取我的数据的项目键,但正如您在 DataSnapshot 函数中看到的那样,我调用尝试获取项目键但它返回空。我正在尝试将所有项目键绑定(bind)到我的数据获取。所以每个获取的项目都应该有它的 key ,这样我就可以使用 key 。但是随后,将获取除项目键之外的所有其他元素。为什么不显示项目 key ?谢谢你的帮助
结构
型号
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);
}
最佳答案
我不确定 item.Key 应该是什么,但你的 Hero 构造函数应该是:
Hero.Add(new Hero(map.Get(""Uid"")?.ToString(),map.Get("Name")?.ToString(), map.Get("Achievement")?.ToString(),map.Get("History")?.ToString(),map.Get("Quote")?.ToString()));
关于c# - 如何从 Firebase 获取 Item Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44868162/