jeudi 30 juillet 2020

.equals Method is not working in if Condition

App does not showing the chatting messages in the physical device when i replace if condition in onDataChanged() method when ever i remove the if condtion in onDataChanged messages are showing but the message is sent to all user not a perticular user

Please do somthing bro i need your help

THIS IS LOGCAT ERROR

2020-07-31 07:58:07.077 26146-26201/com.example.dotchat E/ActivityThread: Failed to find provider info for cn.teddymobile.free.anteater.den.provider
2020-07-31 07:58:07.721 26146-26193/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
2020-07-31 07:58:08.593 26146-26146/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
2020-07-31 07:58:11.863 26146-26146/com.example.dotchat E/RecyclerView: No adapter attached; skipping layout
2020-07-31 07:58:11.864 26146-26146/com.example.dotchat E/RecyclerView: No adapter attached; skipping layout
2020-07-31 07:58:18.571 26146-26146/com.example.dotchat E/RecyclerView: No adapter attached; skipping layout
2020-07-31 07:58:18.571 26146-26146/com.example.dotchat E/RecyclerView: No adapter attached; skipping layout
2020-07-31 07:58:18.787 26146-26146/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
2020-07-31 07:58:19.943 26146-26146/com.example.dotchat E/RecyclerView: No adapter attached; skipping layout
2020-07-31 07:58:19.943 26146-26146/com.example.dotchat E/RecyclerView: No adapter attached; skipping layout
2020-07-31 07:58:24.037 26146-26146/com.example.dotchat E/RecyclerView: No adapter attached; skipping layout

THIS IS MessageActivity.JAVA FILE

package com.example.dotchat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dotchat.Adapter.MessageAdapter;
import com.example.dotchat.Model.Chat;
import com.example.dotchat.Model.User;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;

public class MessageActivity extends AppCompatActivity {

    CircleImageView profile_image;
    TextView username;
    DatabaseReference reference;

    ImageButton btn_send;
    EditText text_send;

    MessageAdapter messageAdapter;
    List<Chat> mchat;

    RecyclerView recyclerView;

    FirebaseUser fuser;
    Intent intent;

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

        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView =findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        profile_image=findViewById(R.id.profile_image);
        username=findViewById(R.id.username);
        btn_send=findViewById(R.id.btn_send);
        text_send=findViewById(R.id.text_send);

        intent=getIntent();
        final String userid=intent.getStringExtra("userid");
        fuser= FirebaseAuth.getInstance().getCurrentUser();

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg=text_send.getText().toString();
                if(!msg.equals("")){
                    sendMessage(fuser.getUid(),userid,msg);
                } else {
                    Toast.makeText(MessageActivity.this,"You can't send Empty message",Toast.LENGTH_SHORT).show();
                }
                text_send.setText("");
            }
        });

        assert userid != null;
        reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                User user=dataSnapshot.getValue(User.class);
                assert user != null;
                username.setText(user.getUsername());
                profile_image.setImageResource(R.mipmap.ic_launcher);

                readMesagges(fuser.getUid(),userid,user.getImageURl());
            }

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

            }
        });
    }

    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("recever",receiver);
        hashMap.put("message",message);

        reference.child("Chats").push().setValue(hashMap);

    }

    private void readMesagges(final String  myid, final String userid, final String imageurl){
        mchat=new ArrayList<>();

        reference=FirebaseDatabase.getInstance().getReference("Chats");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mchat.clear();
                for (DataSnapshot snapshot:dataSnapshot.getChildren()){
                    Chat chat=snapshot.getValue(Chat.class);
                    assert chat != null;
                    if(myid.equals(chat.getReceiver()) && userid.equals(chat.getSender()) ||
                            userid.equals(chat.getReceiver()) && myid.equals(chat.getSender())){
                        mchat.add(chat);

                    }

                    messageAdapter =new MessageAdapter(MessageActivity.this,mchat,imageurl);
                    recyclerView.setAdapter(messageAdapter);


                }


            }

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

            }
        });
    }
}

IN THIS METHOD IF CONDITION IS NOT WORKING(messages are not Showing in the physical device) WHEN EVER I REMOVE THE IF CONDITON NO ERROR SHOWING BUT MESSAGE WILL GOING TO ALL USERS NOT SELCTED RECEIVER

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mchat.clear();
                for (DataSnapshot snapshot:dataSnapshot.getChildren()){
                    Chat chat=snapshot.getValue(Chat.class);
                    assert chat != null;
                    if(myid.equals(chat.getReceiver()) && userid.equals(chat.getSender()) ||
                            userid.equals(chat.getReceiver()) && myid.equals(chat.getSender())){
                        mchat.add(chat);

                    }

                    messageAdapter =new MessageAdapter(MessageActivity.this,mchat,imageurl);
                    recyclerView.setAdapter(messageAdapter);


                }


            }

THIS IS Chat.java file

package com.example.dotchat.Model;

public class Chat {

    private String sender;
    private String receiver;
    private String message;

    public Chat(String sender, String receiver, String message) {
        this.sender = sender;
        this.receiver = receiver;
        this.message = message;
    }

    public Chat() {
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Aucun commentaire:

Enregistrer un commentaire