2
我正在处理消息传递应用程序。现在在对话活动中我想为发件人提供一个选项,可以删除他/她的消息。它的工作正常。问题是我想用alertDialogBox来做到这一点。当发件人长时间按他的信息,然后一个对话框将打开,他可以删除该特定的消息。当我使用改造我能做些什么来删除这种消息?如何从适配器类中的API响应中使用AlertDialogBox删除消息
这里是我的自定义适配器类:
public class Single_chat_adapter extends RecyclerView.Adapter{
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private Context mContext;
private List<Datum2> data;
private String userID;
private UserAuthenticationKey userAuthenticationKey;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
public Single_chat_adapter(Context mContext, List<Datum2> data, String userID) {
this.mContext = mContext;
this.data = data;
this.userID = userID;
}
@Override
public int getItemCount() {
return data.size();
}
// Determines the appropriate ViewType according to the sender of the message.
@Override
public int getItemViewType(int position) {
Datum2 message = (Datum2) data.get(position);
if (message.getOriginatorId().equals(userID)) {
// If the current user is the sender of the message
return VIEW_TYPE_MESSAGE_SENT;
} else {
// If some other user sent the message
return VIEW_TYPE_MESSAGE_RECEIVED;
}
}
// Inflates the appropriate layout according to the ViewType.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_sent, parent, false);
return new SentMessageHolder(view);
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED){
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_received, parent, false);
return new ReceivedMessageHolder(view);
}
return null;
}
// Passes the message object to a ViewHolder so that the contents can be bound to UI.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Datum2 message = (Datum2) data.get(position);
switch (holder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_SENT:
((SentMessageHolder) holder).bind(message);
break;
case VIEW_TYPE_MESSAGE_RECEIVED:
((ReceivedMessageHolder) holder).bind(message);
}
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View view) {
return false;
}
});
}
// Sent message view holder
private class SentMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText;
ImageButton iB;
MsgDltAPIService msgDltAPIService;
String rec_id;
String content_id;
final int[] ids = new int[100];
SentMessageHolder(final View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.text_message_body);
timeText = itemView.findViewById(R.id.text_message_time);
iB = itemView.findViewById(R.id.sentMessageTextDelete);
msgDltAPIService = RestClient.getClient().create(MsgDltAPIService.class);
iB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userAuthenticationKey = new UserAuthenticationKey(mContext.getApplicationContext());
sharedPreferences = mContext.getApplicationContext().getSharedPreferences("user authentication", MODE_PRIVATE);
editor = sharedPreferences.edit();
ids[0] = Integer.parseInt(content_id);
if (!TextUtils.isEmpty(content_id) && TextUtils.isDigitsOnly(content_id)) {
ids[0] = Integer.parseInt(content_id);
} else {
ids[0] = 0;
}
final MsgDltRequest msgDltRequest = new MsgDltRequest(
ids,
rec_id);
Call<MsgDltResponse> call =
msgDltAPIService.msgDlt(userAuthenticationKey.getUserTokenKey(),
msgDltRequest);
call.enqueue(new Callback<MsgDltResponse>() {
@Override
public void onResponse(Call<MsgDltResponse> call, Response<MsgDltResponse> response) {
Toast.makeText(mContext.getApplicationContext(), "message deleted",
Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<MsgDltResponse> call, Throwable t) {
Toast.makeText(mContext.getApplicationContext(), "please try again",
Toast.LENGTH_LONG).show();
}
});
}
});
}
void bind(Datum2 message) {
messageText.setText(message.getMsg());
// Format the stored timestamp into a readable String using method.
timeText.setText(message.getCreatedAt().getFormatTime());
content_id = message.getContentId().toString();
if (! message.getOriginatorId().equals(userID)){
rec_id.valueOf(message.getOriginatorId());
}
else {
rec_id = null;
}
}
}
// Received message view holder
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText, nameText;
// ImageView profileImage;
HexagonImageView profileImage;
ReceivedMessageHolder(View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.text_message_body);
timeText = itemView.findViewById(R.id.text_message_time);
nameText = itemView.findViewById(R.id.text_message_name);
profileImage = itemView.findViewById(R.id.image_message_profile);
}
void bind(Datum2 message) {
messageText.setText(message.getMsg());
// Format the stored timestamp into a readable String using method.
timeText.setText(message.getCreatedAt().getFormatTime());
nameText.setText(message.getOriginator().getFullName());
// Insert the profile image from the URL into the ImageView.
Glide.with(mContext).load("myweburl" + data.get(getLayoutPosition())
.getOriginator().getAvatar()).apply(RequestOptions.circleCropTransform())
.into(profileImage);
}
}
}
我设置了一个名为的iB按钮由我可以删除邮件。但不幸的是,我没有通过使用alertBox来做到这一点。
我真的很抱歉,不明白的地方使用的界面OnChildInteraction和onMessageLongPressed方法。你能给我一个建议吗? –
看到我的编辑,你会明白 –
现在看到更新的代码请 –