C# 带有Unity和Firebase数据库的字典数组-从键获取值
首先,我对Unity还很陌生 我试图从firebase数据库中检索一些数据,将数据存储在字典的数组/列表中,然后使用数组/列表显示来自服务器的数据 所以。。。我的尝试方式:C# 带有Unity和Firebase数据库的字典数组-从键获取值,c#,firebase,unity3d,firebase-realtime-database,C#,Firebase,Unity3d,Firebase Realtime Database,首先,我对Unity还很陌生 我试图从firebase数据库中检索一些数据,将数据存储在字典的数组/列表中,然后使用数组/列表显示来自服务器的数据 所以。。。我的尝试方式: for (int i = 0; i < Global.storesCount; i++) { Transform scrollViewObj = Instantiate(prefab, new Vector3(0, (downSize * i) - firstY, 0), Qu
for (int i = 0; i < Global.storesCount; i++)
{
Transform scrollViewObj = Instantiate(prefab, new Vector3(0, (downSize * i) - firstY, 0), Quaternion.identity);
scrollViewObj.transform.SetParent(scrollContent.transform, false);
scrollViewObj.transform.Find("Overskift").gameObject.GetComponent<Text>().text = Global.offers[i]["Store"] as string;
scrollViewObj.transform.Find("Text (1)").gameObject.GetComponent<Text>().text = Global.offers[i]["Headline"] as string;
scrollViewObj.transform.Find("Text (2)").gameObject.GetComponent<Text>().text = "Din pris: " + Global.offers[i]["Price"] as string + " kr.";
scrollViewObj.transform.Find("Text (3)").gameObject.GetComponent<Text>().text = "Spar: " + Global.offers[i]["AndresPris"] as string + " kr.";
}
1:创建字典数组以保存我的数据:
[System.Serializable]
public class Global
{
public static Dictionary<string, object>[] offers;
}
我得到的只是空值。我在值之后搜索错误吗?或者其他人能看出我做错了什么吗?问题
您的主要问题是Global.offers
是一个数组,因此具有固定的长度,不能动态地放大到这么简单(您每次都必须复制数组!)
您尝试在Handle\u ChildAdded
中解决此问题,但在行中
Global.offers = new Dictionary<string, object>[Global.storesCount+1];
因此,实际上只设置最后一个值。看起来有点像
{null, null, null, null, ..., Dictionary<string, object> }
这是null
解决方案 我建议不要使用数组,而是使用
列表
,与数组相反,它可以动态增长
[System.Serializable]
public class Global
{
public static List<Dictionary<string, object>> offers = new List<Dictionary<string, object>>();
}
要重置列表,否则它会随着数据库的每次加载而变得越来越大。另外,如果您完全确定您只调用了一次,您可能不需要它,我将始终建议您这样做,以获得一个干净的、通用的工作解决方案
然后在中添加元素
void Handle_ChildAdded(object sender, ChildChangedEventArgs e)
{
if (e.DatabaseError != null)
{
Debug.LogError(e.DatabaseError.Message);
return;
}
// Do something with the data in args.Snapshot
if (e.Snapshot.Value != null)
{
var dict = e.Snapshot.Value as Dictionary<string, object>;
if (dict != null)
{
Debug.Log(dict);
Global.offers.Add(dict);
hasHaded = true;
}
}
}
请注意,您的解决方案中也不需要变量
storeCount
,您可以简单地使用
Global.offers.Length
在这个新的解决方案中,您也可以使用
Global.offers.Count
非常感谢你!我会简短地说:我爱你!在同一个代码上看了这么多小时,只需要另一个声明就可以修复。你是个救生员!
[System.Serializable]
public class Global
{
public static List<Dictionary<string, object>> offers = new List<Dictionary<string, object>>();
}
Global.offers.Clear();
void Handle_ChildAdded(object sender, ChildChangedEventArgs e)
{
if (e.DatabaseError != null)
{
Debug.LogError(e.DatabaseError.Message);
return;
}
// Do something with the data in args.Snapshot
if (e.Snapshot.Value != null)
{
var dict = e.Snapshot.Value as Dictionary<string, object>;
if (dict != null)
{
Debug.Log(dict);
Global.offers.Add(dict);
hasHaded = true;
}
}
}
Debug.Log(Global.offers[0]["Store"]);
Global.offers.Length
Global.offers.Count