在聊天应用中从RealtimeDatabase-Firebase检索消息

时间:2019-04-20 08:26:33

标签: android firebase-realtime-database chat

我有CHAT APP,当我显示消息时出现问题该APP有时“在重新打开聊天活动时”在recycleview中仅显示最后1条消息,我不知道代码中的问题在哪里,但是谁能帮助我对此,首先要感谢<3

Intent intent;
List<ChatModel> data;
private RecyclerView mRecy;
ChatAdapter chatAdapter;
String mId;
Button sendmessage;
private EditText mEditWriteMessage;
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
ChatModel chatModel;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mechanic_profile);
    sendmessage = findViewById(R.id.sendmessage);
    mEditWriteMessage = findViewById(R.id.editWriteMessage);
    intent = getIntent();
    mRecy = findViewById(R.id.recy);

    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("CHHAATS");

    data = new ArrayList<>();
    chatAdapter = new ChatAdapter(getApplicationContext(), data);
    mRecy.setHasFixedSize(true);


    mRecy.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    mRecy.setAdapter(chatAdapter);

    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                 chatModel = snapshot.getValue(ChatModel.class);
                }
            if (chatModel.getFrom().equals(firebaseUser.getUid()) && chatModel.getTo().equals(intent.getStringExtra("receiver"))
                    || chatModel.getFrom().equals(intent.getStringExtra("receiver")) && chatModel.getTo().equals(firebaseUser.getUid())) {


                data.add(chatModel);


                mRecy.scrollToPosition(data.size() - 1);

            }
            }
            @Override
            public void onChildChanged (@NonNull DataSnapshot dataSnapshot, @Nullable String s){


                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    chatModel = snapshot.getValue(ChatModel.class);

                }
                if (chatModel.getFrom().equals(firebaseUser.getUid()) && chatModel.getTo().equals(intent.getStringExtra("receiver"))
                        || chatModel.getFrom().equals(intent.getStringExtra("receiver")) && chatModel.getTo().equals(firebaseUser.getUid())) {

                    data.add(chatModel);
                    chatAdapter.notifyDataSetChanged();

                    mRecy.scrollToPosition(data.size() - 1);

                }




            }

            @Override
            public void onChildRemoved (@NonNull DataSnapshot dataSnapshot){

            }

            @Override
            public void onChildMoved (@NonNull DataSnapshot dataSnapshot, @Nullable String s){

            }

            @Override
            public void onCancelled (@NonNull DatabaseError databaseError){

            }
        });

    sendmessage.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick (View v){
            String message = mEditWriteMessage.getText().toString();
            String receiver = intent.getStringExtra("receiver");
            String sender = firebaseUser.getUid();
            meth(message, receiver, sender);
        }
        });
    }

    void meth (String message, String receiver, String sender){

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("CHHAATS");
        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("text", message);
        hashMap.put("from", sender);
        hashMap.put("to", receiver);
        reference.child(receiver).push().setValue(hashMap);


    }


}

关于我的问题,这就是我所能写的...........

0 个答案:

没有答案