I have a list that displays files attached to the letter, this list is in the form of writing a message, and the default is empty, but after the user selects one file from the device’s memory, the list appears and displays the attached files. I have a function to delete a list item, and each list item is beautifully deleted, and the list is immediately updated. But there is one problem - after adding a file to an array, I do not see it in the list of attached files, but it is in the array of attached files. I already tried to clear the list and add elements to it again, tried not to clean anything and then I added two elements, in short, I just did not try, but I didn’t get the list to work properly. Here is the adapter list:

public class AttachedFileAdapter extends RecyclerView.Adapter<AttachedFileAdapter.ViewHolder> { //vars private ArrayList<String> mNames; private ArrayList<Integer> mImageUrls; private Context mContext; public AttachedFileAdapter(Context context, ArrayList<String> names, ArrayList<Integer> imageUrls) { mNames = names; mImageUrls = imageUrls; mContext = context; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.attached_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) { Glide.with(mContext) .asBitmap() .load(mImageUrls.get(position)) .into(holder.image); holder.name.setText(mNames.get(position)); holder.deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { JsonArray array = MySingleton.getInstance().getArray(); if (array.size() > 0) { for (int i = 0; i < array.size(); i++) { JsonObject object = array.get(i).getAsJsonObject(); if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).equals(mNames.get(position))) { array.remove(object); MySingleton.getInstance().setArray(array); // удаляем запись из источника (как я понял) Log.w("MY_TAG", "success"); } notifyDataSetChanged(); } WriteResponseMess.deleteAttachment(position); } } }); } @Override public int getItemCount() { return mImageUrls.size(); } class ViewHolder extends RecyclerView.ViewHolder { ImageView image; TextView name; Button deleteBtn; ViewHolder(View itemView) { super(itemView); image = itemView.findViewById(R.id.image_view); name = itemView.findViewById(R.id.name); deleteBtn = itemView.findViewById(R.id.delete_attachment); } } } 

here's the next file attachment dialog:

  dialog_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @RequiresApi(api = Build.VERSION_CODES.O) @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { File selected = new File(curFolder, fileList.get(position)); if (selected.isDirectory()) { ListDir(selected); } if (selected.isFile()) { if (array.size() == 0) { array = new JsonArray(); array = uploadFiles(array, selected.getName(), convertFileToString(selected.getPath())); } else { if (array.toString().contains(selected.getName())) { Toast.makeText(WriteResponseMess.this, R.string.attaching_message, Toast.LENGTH_SHORT).show(); } else { array = uploadFiles(array, selected.getName(), convertFileToString(selected.getPath())); } } finalDialog1.dismiss(); ms.setArray(array); } } }); 

here is the function of getting the list itself:

 private void getImages() { mNames.clear(); filePreview.clear(); adapter.notifyDataSetChanged(); AttachedFileAdapter itemArrayAdapter = new AttachedFileAdapter(WriteResponseMess.this, mNames, filePreview); RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(WriteResponseMess.this)); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(itemArrayAdapter); for (int i = 0; i < array.size(); i++) { JsonObject object =array.get(i).getAsJsonObject(); if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".txt")) { filePreview.add(R.drawable.ic_txt); } if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".zip")) { filePreview.add(R.drawable.ic_zip); } if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".html")) { filePreview.add(R.drawable.ic_html); } if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".pdf")) { filePreview.add(R.drawable.ic_pdf); } if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".doc")) { filePreview.add(R.drawable.ic_doc); } mNames.add(object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1)); itemArrayAdapter.notifyDataSetChanged(); Log.w("MY_TAG", String.valueOf(mNames)); Integer data = object.get("data").toString().getBytes().length / 1024; } } 

and here is the item delete function:

 public static void deleteAttachment(int adapterPosition) { mNames.remove(adapterPosition); filePreview.remove(adapterPosition); adapter.notifyDataSetChanged(); for (int i = 0; i < array.size(); i++) { JsonObject object = array.get(i).getAsJsonObject(); if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".txt")) { filePreview.add(R.drawable.ic_txt); } if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".zip")) { filePreview.add(R.drawable.ic_zip); } if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".html")) { filePreview.add(R.drawable.ic_html); } if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".pdf")) { filePreview.add(R.drawable.ic_pdf); } if (object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1).endsWith(".doc")) { filePreview.add(R.drawable.ic_doc); } mNames.add(object.get("filename").toString().substring(1, object.get("filename").toString().length() - 1)); adapter.notifyDataSetChanged(); } } 

I can’t understand where I’ve done it and what I did wrong, because logically, everything should work as it should, but I couldn’t achieve it. I hope for the help of more experienced developers, because I myself have been sitting for a week now, and I can’t solve the problem.

  • I did not see where you are updating the adapter associated with RecyclerView . Is there such a line? - Jarvis_J
  • maybe not, I already honestly tried everything I could. I thought that first clean and then fill out the list again, and then notify the adapter about it - this is the adapter update. But most likely I was wrong :) - Andrew Goroshko
  • one
    Well, for the changes to take effect after adding data to the array, you must notify the RV- notifyDataSetChanged(); adapter — notifyDataSetChanged(); . You are updating an adapter , but for some reason it seems to me that it does not belong to the RV). Make: itemArrayAdapter.notifyDataSetChanged(); after adding data to the array. (make adapter and RV global variables). This offhand. - Jarvis_J
  • if I understand correctly, then you need to make everything global, and everywhere where we influence the list insert adapter notification? I just kind of did what you said, but I can't get a normal list, items are not displayed although they are. - Andrew Goroshko
  • in fact yes. Should work. Then I do not know, it is necessary to poke around in all the code - Jarvis_J

0