我正在创建WhatsApp克隆,并且在私人聊天中,其他用户的个人资料图片未显示在选项卡栏的顶部。要么显示默认的个人资料图片,要么如果我从ChatActivity.Java文件中移动这段代码.placeholder(R.drawable.profile_image)
,则什么都不会显示。
我已经反复检查了代码。我删除了行并添加了一些
public class ChatActivity extends AppCompatActivity {
private String messageReceiverID, messageReceiverName, messageReceiverImage, messageSenderID;
private TextView userName, userLastSeen;
private CircleImageView userImage;
private Toolbar ChatToolBar;
private FirebaseAuth mAuth;
private DatabaseReference RootRef;
private ImageButton SendMessageButton;
private EditText MessageInputText;
private final List<Messages> messagesList = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private MessageAdapter messageAdapter;
private RecyclerView userMessagesList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mAuth = FirebaseAuth.getInstance();
messageSenderID = mAuth.getCurrentUser().getUid();
RootRef = FirebaseDatabase.getInstance().getReference();
messageReceiverID = getIntent().getExtras().get("visit_user_id").toString();
messageReceiverName = getIntent().getExtras().get("visit_user_name").toString();
// if(getIntent().getExtras().get("visit_image").toString()!=null) {
// }
InitializeControllers();
DisplayLastSeen();
userName.setText(messageReceiverName);
Picasso.get().load(messageReceiverImage).placeholder(R.drawable.profile_image).into(userImage);
SendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendMessage();
}
});
}
private void InitializeControllers() {
ChatToolBar = (Toolbar) findViewById(R.id.chat_toolbar);
setSupportActionBar(ChatToolBar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = layoutInflater.inflate(R.layout.custom_chat_bar, null);
actionBar.setCustomView(actionBarView);
userImage = (CircleImageView) findViewById(R.id.custom_profile_image);
userName = (TextView) findViewById(R.id.custom_profile_name);
userLastSeen = (TextView) findViewById(R.id.custom_user_last_seen);
SendMessageButton = (ImageButton) findViewById(R.id.send_message_btn);
MessageInputText = (EditText) findViewById(R.id.input_message);
messageAdapter = new MessageAdapter(messagesList);
userMessagesList = (RecyclerView) findViewById(R.id.private_messages_list_of_users);
linearLayoutManager = new LinearLayoutManager(this);
userMessagesList.setLayoutManager(linearLayoutManager);
userMessagesList.setAdapter(messageAdapter);
}
private void DisplayLastSeen() {
RootRef.child("Users").child(messageSenderID)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child("userState").hasChild("state")) {
String state = (dataSnapshot.child("userState").child("state")).getValue().toString();
String date = (dataSnapshot.child("userState").child("date")).getValue().toString();
String time = (dataSnapshot.child("userState").child("time")).getValue().toString();
if (state.equals("online")) {
userLastSeen.setText("Online");
} else if (state.equals("offline")) {
userLastSeen.setText(date + " " + time);
}
} else {
userLastSeen.setText("Offline");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
protected void onStart() {
super.onStart();
RootRef.child("Messages").child(messageSenderID).child(messageReceiverID)
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Messages messages = dataSnapshot.getValue(Messages.class);
messagesList.add(messages);
messageAdapter.notifyDataSetChanged();
userMessagesList.smoothScrollToPosition(userMessagesList.getAdapter().getItemCount());
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void SendMessage() {
String messageText = MessageInputText.getText().toString();
if(TextUtils.isEmpty(messageText)){
Toast.makeText(this, "Enter your message", Toast.LENGTH_SHORT).show();
} else {
String messageSenderRef = "Messages/" + messageSenderID + "/" + messageReceiverID;
String messageReceiverRef = "Messages/" + messageReceiverID + "/" + messageSenderID;
DatabaseReference userMessageKeyRef = RootRef.child("Messages").child(messageSenderID).child(messageReceiverID).push();
String messagePushID = userMessageKeyRef.getKey();
Map messageTextBody = new HashMap();
messageTextBody.put("message", messageText);
messageTextBody.put("type", "text");
messageTextBody.put("from", messageSenderID);
Map messageBodyDetails = new HashMap();
messageBodyDetails.put(messageSenderRef + "/" + messagePushID, messageTextBody);
messageBodyDetails.put(messageReceiverRef + "/" + messagePushID, messageTextBody);
RootRef.updateChildren(messageBodyDetails).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
Toast.makeText(ChatActivity.this, "Message Sent...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ChatActivity.this, "Error. Message not sent", Toast.LENGTH_SHORT).show();
}
MessageInputText.setText("");
}
});
}
}
}
custom_chat_bar.layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/custom_profile_image"
android:layout_width="47dp"
android:layout_height="47dp"
android:layout_marginLeft="230dp"
android:src="@drawable/profile_image"
/>
<TextView
android:id="@+id/custom_profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display UserName"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/cardview_light_background"
/>
<TextView
android:id="@+id/custom_user_last_seen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Seen"
android:layout_below="@+id/custom_profile_name"
android:textStyle="normal"
android:textSize="15sp"
android:textColor="@color/cardview_light_background"
/>
</RelativeLayout>
我想在标签栏左上角的私人聊天中看到个人资料图片