从Android

时间:2017-02-08 23:28:06

标签: android listview firebase firebase-realtime-database android-arrayadapter

我的数据库看起来像这样:

Firebase Databse

我的活动代码是:

    public class FirebaseActivity extends AppCompatActivity {

    ListView lv;

    private FirebaseDatabase database;
    private DatabaseReference myRef;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firebase);

        final List<String> glocations = new ArrayList<String>();

        lv = (ListView) findViewById(R.id.lv);

        database = FirebaseDatabase.getInstance();
        myRef = database.getReference();
        myRef.child("Users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
               Iterable<DataSnapshot> children = dataSnapshot.getChildren();

                for (DataSnapshot child: children){
                    String data = child.getValue(String.class);
                    glocations.add(data);

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,glocations);

        lv.setAdapter(adapter);
    }
}

现在每当我尝试从主活动的设置菜单中打开活动时,我的应用程序崩溃了。我想要它做的就是通过使用带有数组适配器的简单列表,在每次用户在列表视图中更改它们时显示纬度,经度和时间戳。 这是一项不同的活动,而不是主要活动。主要的工作完美。我看过无数的视频,我无法理解我的错误。

我从logcat获得的错误是这个

  

致命的例外:主要                                                                                 处理:com.thanoszoug.geofenceexample,PID:9954                                                                                 com.google.firebase.database.DatabaseException:无法转换   java.util.HashMap类型的值为String                                                                                     在com.google.android.gms.internal.zzbqi.zzaD(未知来源)                                                                                     在com.google.android.gms.internal.zzbqi.zzb(未知来源)                                                                                     在com.google.android.gms.internal.zzbqi.zza(未知来源)                                                                                     在com.google.firebase.database.DataSnapshot.getValue(未知来源)                                                                                     在   com.thanoszoug.geofenceexample.FirebaseActivity $ 1.onDataChange(FirebaseActivity.java:42)                                                                                     在com.google.android.gms.internal.zzbmz.zza(未知来源)                                                                                     在com.google.android.gms.internal.zzbnz.zzYj(未知来源)                                                                                     在com.google.android.gms.internal.zzboc $ 1.run(未知来源)                                                                                     在android.os.Handler.handleCallback(Handler.java:739)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                     在android.os.Looper.loop(Looper.java:158)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:7229)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:2)

我相信这一行会引发异常:

String data = child.getValue(String.class);

getValue将以HashMap的形式为您提供节点&#34;匿名&#34;和其下面的所有其他数据。由于您尝试将其解析为String,因此会抛出此错误:

  

无法将java.util.HashMap类型的值转换为String

获取&#34; Anonymous&#34;的节点级别的原因是你最初将你的听众指向&#34;用户&#34;,并且你通过使用foreach循环来完成它的孩子,并且每个孩子的价值都在&#34; Anonymous&#34;

我建议您重新考虑String的使用情况,此外,如果您希望检索您的位置数据,则应通过.child("Anonymous").child("Location").child("-KcU....")以您自己的自定义方式进行操作。