recylerviewからアイテムを削除しようとしています。アイテムはリストから削除されますが、リスト内で最後に削除されたアイテムのみが表示されます。つまり、リストが正しくリフレッシュされません。すべてのボディは問題に何ができるかを教えてもらえますRecyclerビューの更新の問題
private ArrayList<SentMessageModel> message;
private SentMessageModel mModel;
private LayoutInflater inflater;
private Context context;
private ProgressDialog mDialog;
private View view;
private Set<String> conversationId = new HashSet<>();
public SentMessageAdapter(Context context, ArrayList<SentMessageModel> message) {
this.context = context;
inflater = LayoutInflater.from(context);
this.message = message;
mDialog = new ProgressDialog(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view = inflater.inflate(R.layout.item_message_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
SentMessageModel mSendModel = message.get(position);
holder.nameText.setText(mSendModel.getConversationUsername());
holder.msgText.setText(mSendModel.getSubject());
holder.bodyText.setText(mSendModel.getBody());
Glide.with(context)
.load(ImageLoadingUtil.getProfileImageUrl(mSendModel.getSenderAvatar()))
.error(R.drawable.logo_round)
.into(holder.userImage);
holder.ll_message_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(context, ViewSentMessageActivity.class);
mIntent.putExtra("sent_conversation_id", message.get(position).getConversationId());
context.startActivity(mIntent);
}
});
holder.mCheckBox.setTag(position);
holder.mCheckBox.setChecked(conversationId.contains(String.valueOf(mSendModel.getConversationId())));
holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag();
mModel = message.get(position);
if (holder.mCheckBox.isChecked()) {
conversationId.add(mModel.getConversationId().toString());
} else {
conversationId.remove(mModel.getConversationId().toString());
}
System.out.println("Print conversationId 2 " + conversationId);
}
});
}
public void selectCheckbox(boolean isCheckedState) {
conversationId.clear();
for (int i = 0; i < message.size(); i++) {
if (isCheckedState) {
conversationId.add(message.get(i).getConversationId().toString());
} else {
conversationId.remove(message.get(i).getConversationId().toString());
}
}
notifyDataSetChanged();
}
public void callMoveToTrash() {
if (conversationId.size() > 0) {
String trimmedArray = conversationId.toString();
trimmedArray = trimmedArray.replaceAll(" ", "");
mDialog.setMessage("Please wait...");
mDialog.setCancelable(false);
mDialog.show();
ApiManager.getService().moveToTrash(LocalStorage.getAuthToken(context), trimmedArray)
.enqueue(new Callback<MoveToTrahsModel>() {
@Override
public void onResponse(Response<MoveToTrahsModel> response, Retrofit retrofit) {
if (response.isSuccess() && response.body() != null) {
message.remove(mModel);
notifyDataSetChanged();
conversationId.clear();
mDialog.dismiss();
}
}
@Override
public void onFailure(Throwable t) {
System.out.println("Print ex " + t.getMessage());
t.getMessage();
mDialog.dismiss();
}
});
} else {
((MessageActivity) context).showSnackbar(context.getResources().getString(R.string.select_one_item));
}
}
@Override
public int getItemCount() {
return message.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView nameText, msgText, bodyText;
private RelativeLayout ll_message_row;
private CheckBox mCheckBox;
private ImageView userImage;
public MyViewHolder(View itemView) {
super(itemView);
nameText = (TextView) itemView.findViewById(R.id.nameTextView);
msgText = (TextView) itemView.findViewById(R.id.subjectTextView);
bodyText = (TextView) itemView.findViewById(R.id.bodyTextView);
mCheckBox = (CheckBox) itemView.findViewById(R.id.check_box);
userImage = (ImageView) itemView.findViewById(R.id.user_image);
ll_message_row = (RelativeLayout) itemView.findViewById(R.id.message_row);
}
}
これは、リストから項目を削除するたびに呼び出されます。notifyItemRemoved(i); // i =アイテムの位置 –