简体   繁体   English

Android Firebase Listview ---> Hashmap失败

[英]Android Firebase Listview ---> Hashmap Fail

I am trying to retrieve the Restaurant Name data from Firebase and output them in individual lines on ListView. 我正在尝试从Firebase检索“餐厅名称”数据,并在ListView的各个行中输出它们。 I created a sample of the data which only consists of numbers(strings). 我创建了仅包含数字(字符串)的数据示例。

Retrieving the data seems fine as I could output them line by line in console, but my Hashmap is saving everything into the same "node" or "field" 检索数据似乎很好,因为我可以在控制台中逐行输出它们,但是我的Hashmap将所有内容保存到相同的“节点”或“字段”中

Can anyone help me understand what I did wrong? 谁能帮助我了解我做错了什么?

@Override
        public void onDataChange(DataSnapshot Snapshot) {
            int x = 1;
            //Do some stuff once
            for (DataSnapshot RestNames : Snapshot.getChildren()) {
                name = RestNames.getValue().toString();
                //System.out.println(name);
                map.put(x, name);
                x=x+1;
            }
            System.out.println(map);
            Items.add(map);
            System.out.println(Items);
            listView.setAdapter(mgadapter);
        }

The Output in the console is as follows : 控制台中的输出如下:

 {8=3456, 11=9, 9=34567, 5=3, 3=3, 4=4, 10=0, 1=1, 7=345, 6=34, 2=2}

Android emulator shows the same value for every single row. Android模拟器为每一行显示相同的值。

I want to display each value on a separate row. 我想在单独的行上显示每个值。

Thank you! 谢谢!

EDIT: SNIPPET OF JSON 编辑:JSON的片段

{
 "Eat": {
  "Name": {
   "-Jy3yehAkgqhg4knlxx_": "1",
   "-Jy3yjQT2AxtZMqD2kov": "2",
   "-Jy3yk96Mo5MKOEEzviJ": "3",
   "-Jy3yksamL08R0BckxNZ": "4",
   "-Jy5JBJYZUTxZQtmdDmi": "3",
   "-Jy5JIXT_lDZrUOkF3T1": "34",
   "-Jy5JJ0oMqGrs2vfFge2": "345",
   "-Jy5JJTyET830PYOT3yA": "3456",
   "-Jy5JJu-jDGMDXncWDKf": "34567",
   "-Jy5JVejdsUtggM8vBoi": "0",
   "-Jy5JbwEoWrKAi6XIVQY": "9"
  }
 }
}

Since we're missing some code in your snippet, I completed it an ran it locally: 由于您的代码段中缺少一些代码,因此我完成了它并在本地运行:

Firebase ref = new Firebase("https://stackoverflow.firebaseio.com/32367022/Eat/Name");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        int x = 1;
        Map<Integer,Object> map = new HashMap<Integer, Object>();
        for (DataSnapshot child: snapshot.getChildren()) {
            String name = child.getValue().toString();
            System.out.println(name);
            map.put(x, name);
            x=x+1;
        }
        System.out.println(map);
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
    }
});

This prints out the following for me: 这为我打印出以下内容:

1
2
3
4
3
34
345
3456
34567
0
9
{1=1, 2=2, 3=3, 4=4, 5=3, 6=34, 7=345, 8=3456, 9=34567, 10=0, 11=9}

The first lines show the output from inside the for loop. 第一行显示了for循环内部的输出。 The last lines shows the HashMap . 最后HashMap行显示了HashMap

I use the most basic of Java classes, and this behavior seems correct to me. 我使用了最基本的Java类,这种行为对我来说似乎是正确的。

I expect that the System.out.println(name); 我希望System.out.println(name); inside the for loop will display the same output as above for you, because the Firebase SDK handles ordering there. for循环内for将为您显示与上面相同的输出,因为Firebase SDK在那里处理订购。

If the order in the loop is correct, either your map or your Items object changes the order. 如果循环中的顺序正确,则您的mapItems对象都会更改顺序。 It is impossible to say without seeing the types of these. 不看这些类型就不可能说。

But in general, the approach above is how you should troubleshoot this problem: isolate it to a single object/class and then either fix what's wrong with that class (if it's your code) or replace it with a class that works for your use-case (if it comes from a library). 但总的来说,以上方法是您应该如何解决此问题的方法:将其隔离到单个对象/类,然后修复该类的问题(如果是您的代码)或将其替换为适合您的用途的类-大小写(如果来自库)。

Update: I created a minimal app that shows the numbers in the correct order in a list view in this Github repo . 更新:我创建了一个最小的应用程序,该应用程序在此Github存储库的列表视图中以正确的顺序显示数字。

在模拟器中运行的应用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM