Failed to convert value of type java.util.HashMap to String. Converting string to map

huangapple go评论73阅读模式
英文:

Failed to convert value of type java.util.HashMap to String. Converting string to map

问题

以下是我的代码。我遇到了这个错误:“com.google.firebase.database.DatabaseException: 无法将类型为java.util.HashMap的值转换为String”。请帮忙。

du = FirebaseDatabase.getInstance().getReference().child("storename");
du.keepSynced(true);
du.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
        };
        Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
        name.setText(map.get("ownerName"));
        storename.setText(map.get("compName"));
        storeaddr.setText(map.get("address"));
        ph1.setText("Ph: " + map.get("phoneNumber"));
        gst.setText(map.get("gstNum"));
    }

    @Override
    public void onCancelled(DatabaseError error) {
    }
});

Failed to convert value of type java.util.HashMap to String. Converting string to map

英文:

Below is my code. I get this error "com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String" . Please help.

 du = FirebaseDatabase.getInstance().getReference().child(&quot;storename&quot;);
    du.keepSynced(true);
    du.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            GenericTypeIndicator&lt;Map&lt;String, String&gt;&gt; genericTypeIndicator = new GenericTypeIndicator&lt;Map&lt;String, String&gt;&gt;() {
            };
            Map&lt;String, String&gt; map = dataSnapshot.getValue(genericTypeIndicator);
            name.setText(map.get(&quot;ownerName&quot;));
            storename.setText(map.get(&quot;compName&quot;));
            storeaddr.setText(map.get(&quot;address&quot;));
            ph1.setText(&quot;Ph: &quot; + map.get(&quot;phoneNumber&quot;));
            gst.setText(map.get(&quot;gstNum&quot;));
        }

        @Override
        public void onCancelled(DatabaseError error) {
        }
    });

Failed to convert value of type java.util.HashMap to String. Converting string to map

答案1

得分: 0

你正在从 /storename 加载所有数据。由于可能有多个商店在其中,你需要循环遍历 dataSnapshot.getChildren() 来访问每个独立的商店。

因此:

du = FirebaseDatabase.getInstance().getReference().child("storename");
du.keepSynced(true);
du.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
            GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
            };
            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
            name.setText(map.get("ownerName"));
            storename.setText(map.get("compName"));
            storeaddr.setText(map.get("address"));
            ph1.setText("Ph: " + map.get("phoneNumber"));
            gst.setText(map.get("gstNum"));
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {
        throw error.toException(); // 永远不要忽视错误
    }
});
英文:

You're loading all data from /storename . Since there may be multiple stores under there, you'll need to loop over dataSnapshot.getChildren() to get to each individual store.

So:

du = FirebaseDatabase.getInstance().getReference().child(&quot;storename&quot;);
du.keepSynced(true);
du.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
            GenericTypeIndicator&lt;Map&lt;String, String&gt;&gt; genericTypeIndicator = new GenericTypeIndicator&lt;Map&lt;String, String&gt;&gt;() {
            };
            Map&lt;String, String&gt; map = dataSnapshot.getValue(genericTypeIndicator);
            name.setText(map.get(&quot;ownerName&quot;));
            storename.setText(map.get(&quot;compName&quot;));
            storeaddr.setText(map.get(&quot;address&quot;));
            ph1.setText(&quot;Ph: &quot; + map.get(&quot;phoneNumber&quot;));
            gst.setText(map.get(&quot;gstNum&quot;));
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {
        throw error.toException(); // never ignore errors
    }
});

huangapple
  • 本文由 发表于 2020年9月28日 00:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64090511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定