2017-07-02 169 views
2

我想在我的数据库中获取我的数据的项目密钥,但正如您在DataSnapshot函数中看到的那样,我试图获取项目密钥,但它返回空白。我正试图将所有项目键绑定到我的数据提取。所以每个取得的物品都应该有钥匙,所以我可以使用钥匙。但是,除了项目键以外,所有其他元素都被提取。为什么项目键不显示?感谢您的帮助如何从Firebase获取项目密钥

结构

enter image description here

型号

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); 

     } 

回答

0

好的。我找到了答案。这可能有助于未来的人。

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(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); 


     } 
+0

问题是什么?需要调用'Key.ToString()'如果是这种情况,为什么键没有打印在'Console.WriteLine(“发送项目键”+ item.Key.ToString());' –

+0

我试图绑定项目在foreach循环中关键每个数据调用 – Switz

1

我不知道什么item.Key应该是,但你的英雄的构造应该是:

Hero.Add(new Hero(map.Get(""Uid"")?.ToString(),map.Get("Name")?.ToString(), map.Get("Achievement")?.ToString(),map.Get("History")?.ToString(),map.Get("Quote")?.ToString())); 
+0

请看看我的数据库的结构。我试图获得该项目的关键是... ..........不是用户的用户 – Switz

+0

您使用的是哪个版本?你有没有尝试'getKey()'或'Key()。“不同版本的文档有不同的方法。另外你的控制台输出是什么? –

+0

我正在使用VS,Xamarin,所以我使用密钥。在控制台中,我得到了所有的钥匙。意思是当我要求一个英雄的使用权时,它应该返回英雄的钥匙而不是用户的ID,但是它返回空。 – Switz

1

的原因你没有得到,关键是因为你正在寻找它在错误的数据快照。

取而代之的是以下内容: -

public void OnDataChange(DataSnapshot snapshot) 
    { 
     var items = snapshot.Children?.ToEnumerable<DataSnapshot>(); 

     HashMap map; 
     foreach (DataSnapshot item in items) 
     { 
      key = item.Key 

     .... 

使用此: -

public void OnDataChange(DataSnapshot snapshot) 
{ 
    var items = snapshot.Children?.ToEnumerable<DataSnapshot>(); 
    var key = snapshot.Key 

    HashMap map; 
    foreach (DataSnapshot item in items) 
    { 
    .... 

这里的逻辑是,当你调用foreach(Datasnapshot item in items),那么你遍历父Datasnapshot的孩子。取而代之,您需要使用onDataChange(Datasnapshot snapshot)方法中获取的数据快照的密钥。这是snapshot。因此,您应该致电.Keysnapshot

+0

当我调用'var key = snapshot.Key'时,它给了我'Hero',这是结构的名称而不是键 – Switz