gpt4 book ai didi

android - 如何使用适配器类中的 api 响应使用 AlertDialogBox 删除消息

转载 作者:行者123 更新时间:2023-11-29 02:37:55 25 4
gpt4 key购买 nike

我正在开发一个消息传递应用程序。现在在对话 Activity 中,我想为发件人选择一个可以删除他/她的消息的选项。我用一个按钮来完成它并且它工作正常。问题是我想用 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 来做到这一点。

最佳答案

在您长按的监听器中,使用监听器将消息对象传递给父 Activity/适配器,然后从父打开一个对话框将对象传递给它,现在删除数据时只需从 List<Datum2> data 中删除消息并通知适配器

public class SingleChatAdapter extends RecyclerView.Adapter {
// ...
//...
@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) {
mListener.onMessageLongPressed(message);
return false;
}
});
}
// ....
// ...

// here is the listener interface parent need to implement
public interface OnChildInteraction {
public void onMessageLongPressed(Datum2 message);
}
}

parent 的粗略实现将是

public class ParentActicvity extends AppCompatActivity implements SingleChatAdapter.OnChildInteraction
@override
public void onMessageLongPressed(Datum2 message){
// override delete button listener to call list.remove(message)
// notify adapter.
// open dialog

}

关于android - 如何使用适配器类中的 api 响应使用 AlertDialogBox 删除消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45931232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com