Android chat is not updating in second Device(user-2) after adding child object in first device (user-1) using firebase database and recyclerview-ThrowExceptions

Exception or error:

I am creating chat between two user using fire-base real time database without push notification or any services, the problem i am facing is that on sending text to user-2 device from user-1 is not updating chat… On chatAdapter.notifyDataSetChanged()… All other is working fine.
Note:: If I do refresh the activity whole chat is occurring properly.
Here is my onButtonClickCode::

private void sendToDBIndirectly(final Chat chat, String tag) {
    final DatabaseReference chatRef = FirebaseDatabase.getInstance().getReference().child( "Chat" ).child( "/" + tag );
    chatRef.addListenerForSingleValueEvent( new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (!check) {
                if (dataSnapshot.getChildrenCount() <= 0) {
                    chatRef.child( "1" ).setValue( chat );
                    chats.add( chat );

                } else {
                    long count = dataSnapshot.getChildrenCount();
                    count++;
                    chatRef.child( String.valueOf( count ) ).setValue( chat );
                    chats.add( chat );

                }

                Collections.sort( chats, new Comparator<Chat>() {
                    DateFormat f = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

                    @Override
                    public int compare(Chat lhs, Chat rhs) {
                        try {
                            return f.parse( lhs.getTime() ).compareTo( f.parse( rhs.getTime() ) );
                        } catch (ParseException e) {
                            throw new IllegalArgumentException( e );
                        }
                    }
                } );
                chatAdapter = new ChatAdapter( ChatActivity.this, chats, "" );
                LinearLayoutManager layoutManager = new LinearLayoutManager( ChatActivity.this );
                layoutManager.setStackFromEnd(true);
                chatHolder.setLayoutManager(layoutManager );
                chatHolder.setAdapter( chatAdapter );
                chatHolder.scrollToPosition( chats.size() - 1 );
                chatAdapter.notifyDataSetChanged();

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

        }
    } );
} 

This is my Adapter::

package myproject.com.reuseableitems.adapters;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.Chat_ViewHolder> {

    private Context context;
    private ArrayList<Chat> chats;
    private String flag;

    public ChatAdapter(Context context, ArrayList<Chat> chats, String flag) {
        this.context = context;
        this.chats = chats;
        this.flag = flag;
    }

    @NonNull
    @Override
    public Chat_ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new Chat_ViewHolder( LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.adapter_chat, viewGroup, false ) );
    }

    @Override
    public void onBindViewHolder(@NonNull Chat_ViewHolder viewHolder, int i) {
            viewHolder.setImageData( chats.get(i) );
    }


    @Override
    public int getItemCount() {
        return chats.size();
    }

    class Chat_ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout customer, seller;
        TextView customerText, sellerText;

        Chat_ViewHolder(View itemView) {
            super( itemView );
            customer = itemView.findViewById( R.id.customer );
            seller = itemView.findViewById( R.id.seller );
            customerText = itemView.findViewById( R.id.customerText );
            sellerText = itemView.findViewById( R.id.senderText );
        }

        void setImageData(Chat chat) {

            SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm:ss", Locale.getDefault() );
            if (chat.getTag().equals( "customer" )) {
                customer.setVisibility( View.VISIBLE );
                customerText.setText( chat.getMessage() + "\n" + chat.getTime() );

            } else if (chat.getTag().equals( "seller" )) {
                seller.setVisibility( View.VISIBLE );
                sellerText.setText( chat.getMessage() + "\n" + chat.getTime() );
            }


        }
    }
}

Firebase Chat record::

user-1 Chat view 1
user-2 Chat view 2

chat in android device onCreate Activity
mobile view for chat

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *