HashMap不会编写和读取我想要的所有内容

时间:2019-07-07 11:46:51

标签: android firebase-realtime-database hashmap

我正在尝试使用Hashmap在我的数据库中编写当用户想要发送消息时的接收者,消息和发送者,但是问题是只有消息和发送者才写在数据库中。问题在于该应用程序将无法访问接收者的主键。

我试图更改btn_send onClick的顺序,但没有任何改变。我正在考虑在intent.getStringExtra中添加另一个字符串。也许是这样。

MessageActivity.java:

        intent=getIntent ();
        final String userid=intent.getStringExtra("FIRST NAME ");

        fuser = FirebaseAuth.getInstance ().getCurrentUser();
        reference = FirebaseDatabase.getInstance ().getReference("users");
        reference.addValueEventListener ( new ValueEventListener() {
            @Override
            public void onDataChange (@NonNull DataSnapshot dataSnapshot) {

                for(DataSnapshot snapshot:dataSnapshot.getChildren ()){
                    User user=snapshot.getValue (User.class);

                    assert user != null;
                    assert fuser != null;
                    if(!fuser.getUid ().equals(user.getFirstName())){
                        username.setText (user.getFirstName());
                    }
                }
            }

            @Override
            public void onCancelled (@NonNull DatabaseError databaseError) {}
        });
        btn_send.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick (View view) {
                String msg=text_send.getText().toString();
                if(!msg.equals("")){
                    sendMessage(fuser.getUid(), userid, msg);
                } else {
                    Toast.makeText(MessageActivity.this,"You can't send empty", Toast.LENGTH_SHORT).show();
                }
                text_send.setText("");
            }
        } );
        }
private void sendMessage(String sender, String receiver, String message)
{
    DatabaseReference reference = FirebaseDatabase.getInstance ().getReference ();
    HashMap<String,Object> hashMap=new HashMap<>();
    hashMap.put("sender", sender);
    hashMap.put("receiver", receiver);
    hashMap.put("message", message);
    reference.child("Chats").push().setValue(hashMap);
}

调试器向我显示:

/ViewRootImpl@d19ed8[MessageActivity]: ViewPostIme pointer 0
D/ViewRootImpl@d19ed8[MessageActivity]: ViewPostIme pointer 1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@ab6a231[MessageActivity]
    getNavigationBarColor() -855310
V/InputMethodManager: Starting input: tba=com.example.sportsbuddy ic=com.android.internal.widget.EditableInputConnection@5a5e0d0 mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
D/InputMethodManager: startInputInner - Id : 0
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=105
    Input channel destroyed: fd=108
W/IInputConnectionWrapper: getExtractedText on inactive InputConnection

我希望将接收者的主密钥放入数据库中。

0 个答案:

没有答案