我正在尝试删除 Firebase 实时数据库中用户之间的消息。我能够成功删除消息,但我想添加一个条件,即用户无法删除他收到的消息,只能删除他发送的消息,目前用户能够删除他收到的消息以及他从firebase实时数据库中发送的消息。
我的代码
MessageActivity.java
public class MessageActivity extends AppCompatActivity {
CircleImageView profile_image;
TextView user_name;
FirebaseFirestore fstore;
FirebaseAuth fAuth;
FirebaseUser fuser;
DatabaseReference reference;
Toolbar toolbar;
ImageButton imageButton;
EditText textsend;
String userID;
MessageAdapter messageAdapter;
List<Chat> mChat;
RecyclerView recyclerView;
DatabaseReference Chatreference;
String TAG = "MyTag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
fAuth = FirebaseAuth.getInstance();
fstore = FirebaseFirestore.getInstance();
userID = fAuth.getCurrentUser().getUid();
profile_image = findViewById(R.id.profile_image);
user_name = findViewById(R.id.username);
imageButton = findViewById(R.id.btn_send);
textsend = findViewById(R.id.text_send);
recyclerView = findViewById(R.id.recycler_view2);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
toolbar = findViewById(R.id.myToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
Intent intent = getIntent();
String userid = intent.getStringExtra("userid");
fuser = FirebaseAuth.getInstance().getCurrentUser();
//String username = intent.getStringExtra("UserName");
User user=new User();
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
user_name.setText(user.getFirst());
Log.d(TAG, "Single Event Listener called");
if (user.getImageURL().equals("default")) {
profile_image.setImageResource(R.mipmap.ic_launcher);
} else {
//and this
Glide.with(getApplicationContext()).load(user.getImageURL()).into(profile_image);
}
readMessages(fuser.getUid(), userid, user.getImageURL());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = textsend.getText().toString();
textsend.setText("");
if (!msg.equals("")) {
sendMessage(fuser.getUid(), userid, msg);
} else {
Toast.makeText(MessageActivity.this, "Empty messages cant be send", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
User user=new User();
//readMessages(fuser.getUid(), userid, user.getImageURL());
}
private void sendMessage(String sender, final String receiver, String message) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", sender);
hashMap.put("receiver", receiver);
hashMap.put("message", message);
DatabaseReference Chatreference = FirebaseDatabase.getInstance().getReference("Chats");
String key = Chatreference.push().getKey();
Chatreference.child(key).setValue(hashMap);
}
private void readMessages(String myid, String userid, String imageurl) {
mChat = new ArrayList<>();
Chatreference = FirebaseDatabase.getInstance().getReference("Chats");
Chatreference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Chat chat = dataSnapshot.getValue(Chat.class);
if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid) ||
chat.getReceiver().equals(userid) && chat.getSender().equals(myid)) {
chat.setMessageId(dataSnapshot.getKey());
mChat.add(chat);
Log.d(TAG, "Child_Event_Listener_Called");
Log.d(TAG, "msg " + chat.getMessage());
}
messageAdapter = new MessageAdapter(MessageActivity.this, mChat, imageurl);
recyclerView.setAdapter(messageAdapter);
messageAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
Chat chat = dataSnapshot.getValue(Chat.class);
chat.setMessageId(dataSnapshot.getKey());
mChat.remove(chat);
messageAdapter.notifyDataSetChanged();
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
MessageAdapter.java
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;
String imageurl;
FirebaseUser fuser;
public MessageAdapter(Context mContext, List<Chat> mChat,String imageurl) {
this.mContext = mContext;
this.mChat = mChat;
this.imageurl=imageurl;
}
@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 MessageAdapter.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);
}
holder.show_message.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String message_id=chat.getMessageId();
Task<Void> task = Utils.removeUser(message_id);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(mContext,"Message Deleted",Toast.LENGTH_SHORT).show();
}
});
return true;
}
});
}
@Override
public int getItemCount() {
return mChat.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView show_message;
public ImageView profile_image;
public ViewHolder(@NonNull 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;
}
}
}
聊天.java
public class Chat {
private String sender;
private String receiver;
private String message;
String MessageId;
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;
}
public String getMessageId() {
return MessageId;
}
public void setMessageId(String messageId) {
MessageId = messageId;
}
@Override
public boolean equals(@Nullable Object obj) {
if(obj instanceof Chat){
Chat chat=(Chat) obj;
return this.MessageId.equals(chat.getMessageId());}
else
return false;
}
}
基于您的Chat
对象,您有发送者
和接收者
Ids。因此,您可以在onBindViewHolder
内部执行以下操作
if (chat.getSender().equals(fuser.getUid())){
// enable/show delete button
}
else{
// disable/hide delete button
}
我仍在开发我的登录应用程序。此时,用户可以注册、登录和更新配置文件信息。数据存储在firebase实时数据库中,该装置也通过firebase工作。我想添加一个“删除配置文件”按钮,以便从firebase中删除用户信息。我弄清楚了如何从自动认证过程中删除用户。现在我尝试对Uid调用remove()函数,因此现在数据在实时数据库中被删除,但仍在Firebase身份验证中,如果删除userdata,应用
我如何删除记录“苹果”(在图片中标记)? 根据文档,要移除一个项,需要对引用调用removeValue()。但要获得引用,我需要子ID。因为它是一个随机生成的id(-kisnx87ayigsh3ilp0d),如何删除它?
我正在开发一个社交网络应用程序,用户可以关注其他用户,喜欢他们的帖子、评论。。。当某个用户跟踪某人时,它会在另一个用户帐户的通知片段中显示,通知他有一个新的跟踪者。问题是,当用户点击unfollow时,我无法删除通知。以下是我尝试过的: 下面是我添加通知的方式: 我的代码的问题是,每当用户取消关注某人时,该用户的所有通知都会被删除,包括他的喜欢和评论。我想要的是删除“开始关注你”。以下是它在Fir
我以为上面的办法行得通,但它失败了。我把它放在渲染行部分。它出现了reference.remove失败。第一个参数必须是有效的函数。就像在这些房间里,它有信息,我认为这是我出错的地方,但我不知道如何去做它。谢谢。
我在Android应用程序中使用Firebase实时数据库,并且有如下数据: 数据库映像 我想删除周末所有用户的“点”:选项卡。每个周末自动删除。我该怎么做呢。
我对Android dev和Firebase很陌生。 我需要以下帮助: 提前谢谢你。 公共类AddUsername扩展片段实现View.OnClickListener,TextWatcher{