chat application ne marche pas
Bonjour la communauté
j'espère que ce post vous trouve dans la bonne santé
je suis entrain de développez une application de chat entre des utilisateures stocké dans firebase en android mais ça fait deux jours que je n'arrive pas à obtenir des bonnes résultats
je veux tout d'abord lister tout les utilisateurs trouvé dans dans le firebase , aprés quand je clique sur un utilisateur bien spécifié , je l'envoie un msg
voici mon code
UserAdapter ==> pour lister les users trouvés dans firebase
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
package com.example.projet_v1.Adapters;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.projet_v1.ChatActivity;
import com.example.projet_v1.Model.User;
import com.example.projet_v1.R;
import java.util.List;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> mUsers;
public UserAdapter(Context mContext , List<User> mUsers) {
this.mContext=mContext;
this.mUsers= mUsers;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView username;
public ImageView profile_image;
public ViewHolder(View itemView){
super(itemView);
username= itemView.findViewById(R.id.username);
profile_image = itemView.findViewById(R.id.profile_image);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item , parent, false);
return new UserAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final User user = mUsers.get(position);
holder.username.setText(user.getUser_nom());
if (user.getImageURL().equals("default")) {
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(mContext).load(user.getImageURL()).into(holder.profile_image);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ChatActivity.class);
intent.putExtra("userid", user.getUser_id());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount(){
return mUsers.size();
}
} |
UserFragements
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package com.example.projet_v1.messagerieFragements;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.example.projet_v1.Adapters.UserAdapter;
import com.example.projet_v1.Model.User;
import com.example.projet_v1.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
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.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class UsersFragment extends Fragment {
private static final String TAG = "chat";
private RecyclerView recyclerView;
private UserAdapter userAdapter;
private List<User> mUsers;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_users , container , false);
recyclerView = view.findViewById(R.id.recycler_viewa);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mUsers = new ArrayList<>();
readUsers();
userAdapter = new UserAdapter(getContext(), mUsers);
recyclerView.setAdapter(userAdapter);
// Inflate the layout for this fragment
return view;
}
private void readUsers() {
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
User user = ds.getValue(User.class);
assert user != null;
assert firebaseUser != null;
if (!user.getUser_id().equals(firebaseUser.getUid())) {
mUsers.add(user);
}
Log.d(TAG, "Value is: " + mUsers);
}
userAdapter = new UserAdapter(getContext(), mUsers);
recyclerView.setAdapter(userAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
} |
MessageAdapter
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
package com.example.projet_v1.Adapters;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.projet_v1.ChatActivity;
import com.example.projet_v1.Model.Chat;
import com.example.projet_v1.Model.User;
import com.example.projet_v1.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.List;
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
public static final int MSG_TYPE_LEFT = 0;
public static final int MSG_TYPE_RIGHT = 1;
private Context mContext;
private List<Chat> mChat;
private String imageurl;
FirebaseUser fuser;
public MessageAdapter(Context mContext , List<Chat> mChat , String imageurl) {
this.mContext=mContext;
this.mChat= mChat;
this.imageurl= imageurl;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView show_message;
public ImageView profile_image;
public ViewHolder(View itemView){
super(itemView);
show_message= itemView.findViewById(R.id.show_message);
profile_image = itemView.findViewById(R.id.profile_image);
}
}
@Override
public int getItemViewType(int position) {
fuser = FirebaseAuth.getInstance().getCurrentUser();
if(mChat.get(position).getSender().equals(fuser.getUid())){
return MSG_TYPE_RIGHT;
}else
{
return MSG_TYPE_LEFT;
}
}
@NonNull
@Override
public MessageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(viewType == MSG_TYPE_RIGHT){
View view= LayoutInflater.from(mContext).inflate(R.layout.chat_item_right , parent, false);
return new MessageAdapter.ViewHolder(view);
}else {
View view= LayoutInflater.from(mContext).inflate(R.layout.chat_item_left , parent, false);
return new MessageAdapter.ViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Chat chat= mChat.get(position);
holder.show_message.setText(chat.getMessage());
if (imageurl.equals("default")){
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
}else {
Glide.with(mContext).load(imageurl).into(holder.profile_image);
}
}
@Override
public int getItemCount(){
return mChat.size();
}
} |
Ma classe Chat
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
package com.example.projet_v1.Model;
public class Chat {
private String sender;
private String receiver;
private String message;
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;
}
public Chat() {
}
public Chat(String sender, String receiver, String message) {
this.sender = sender;
this.receiver = receiver;
this.message = message;
}
} |
ma classe User
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
package com.example.projet_v1.Model;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.Date;
public class User {
public Date getUsr_date_naissance() {
return usr_date_naissance;
}
public void setUsr_date_naissance(Date usr_date_naissance) {
this.usr_date_naissance = usr_date_naissance;
}
private String user_id;
private String user_nom;
private String user_prenom;
private String user_email;
private String user_password;
private String user_tel;
private String user_sex;
private Date usr_date_naissance;
private String ImageURL;
public String getImageURL() {
return ImageURL;
}
public void setImageURL(String imageURL) {
ImageURL = imageURL;
}
private String usr_img_url;
public static DatabaseReference databaseUsers = FirebaseDatabase.getInstance().getReference("users");
public User(){
}
public User(String user_id, String user_nom, String user_prenom, String user_email, String user_password, String user_tel, String user_sex, Date usr_date_naissance, String imageURL, String usr_img_url) {
this.user_id = user_id;
this.user_nom = user_nom;
this.user_prenom = user_prenom;
this.user_email = user_email;
this.user_password = user_password;
this.user_tel = user_tel;
this.user_sex = user_sex;
this.usr_date_naissance = usr_date_naissance;
ImageURL = imageURL;
this.usr_img_url = usr_img_url;
}
public String getUsr_img_url() {
return usr_img_url;
}
public void setUsr_img_url(String usr_img_url) {
this.usr_img_url = usr_img_url;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_nom() {
return user_nom;
}
public void setUser_nom(String user_nom) {
this.user_nom = user_nom;
}
public String getUser_prenom() {
return user_prenom;
}
public void setUser_prenom(String user_prenom) {
this.user_prenom = user_prenom;
}
public String getUser_email() {
return user_email;
}
public void setUser_email(String user_email) {
this.user_email = user_email;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
public String getUser_tel() {
return user_tel;
}
public void setUser_tel(String user_tel) {
this.user_tel = user_tel;
}
public String getUser_sex() {
return user_sex;
}
public void setUser_sex(String user_sex) {
this.user_sex = user_sex;
}
public void add_user_todatabase(String id){
//String id = databaseUsers.push().getKey();
this.setUser_id(id);
databaseUsers.child(id).setValue(this);
}
} |
ChatActivity
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
package com.example.projet_v1;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.projet_v1.Adapters.MessageAdapter;
import com.example.projet_v1.Model.Chat;
import com.example.projet_v1.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 de.hdodenhof.circleimageview.CircleImageView;
public class ChatActivity extends AppCompatActivity {
CircleImageView profile_image;
TextView username;
FirebaseUser fuser;
DatabaseReference reference;
Intent intent;
ImageButton btn_send;
EditText text_send;
MessageAdapter messageAdapter ;
List<Chat> mChat;
RecyclerView recyclerView;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar2);
// Toolbar toolbar = findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
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.txt_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(ChatActivity.this , "You can't send empty message" , Toast.LENGTH_SHORT).show();
}
text_send.setText("");
}
});
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUser_nom());
if(user.getImageURL().equals("default")){
profile_image.setImageResource(R.mipmap.ic_launcher);
}else {
Glide.with(ChatActivity.this).load(user.getImageURL()).into(profile_image);
}
readMessages(fuser.getUid() , userid ,user.getImageURL());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
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);
}
private void readMessages (final String myid , final String userid ,final String imageurl ){
mChat = new ArrayList<>();
reference =FirebaseDatabase.getInstance().getReference("chat");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mChat.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Chat chat = ds.getValue(Chat.class);
if (chat.getReceiver().equals(myid) && chat.getSender().equals(myid) || chat.getReceiver().equals(userid) && chat.getSender().equals(myid)) {
mChat.add(chat);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
} |
Activity aui contient les deux fragement
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
package com.example.projet_v1;
import android.os.Bundle;
import android.widget.TableLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.projet_v1.messagerieFragements.ChatFragment;
import com.example.projet_v1.messagerieFragements.UsersFragment;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
public class messagerie extends AppCompatActivity {
private ViewPagerAdapter viewPagerAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messagerie);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
//add fragment here
viewPagerAdapter.addFragment(new ChatFragment(),"Chat");
viewPagerAdapter.addFragment(new UsersFragment(),"Users");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
} |