I have a dialog in the application that displays a list of files attached to the letter. The list is filled with data from the array in which there are elements in json format. Next, I try to delete it by swipe. If I understand correctly, it will remove not only the element from the array but also the element from the list. Everything seems fine, but there are a few problems. When I delete an item, Iβm left an empty space in the list and the list is not updated. And after re-calling the dialog with the list, I donβt fill the list at all, although the logs show that the array is not empty. Here is how I invoke a dialog with a list of attachments:
attachment.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (array.size() > 0) { if (movieList.size() > 0) { movieList.clear(); showDialog(SHOW_ATTACHED_FILES); prepareList(); } else { showDialog(SHOW_ATTACHED_FILES); prepareList(); } } else { Toast.makeText(WriteResponseMess.this, "NO attached files", Toast.LENGTH_SHORT).show(); } return true; } }); and here is the construction of a dialogue:
dialog = new Dialog(WriteResponseMess.this); dialog.setContentView(R.layout.show_attachment); dialog.setCanceledOnTouchOutside(true); final RecyclerView recyclerView = dialog.findViewById(R.id.lv); final AttachmentAdapter mAdapter = new AttachmentAdapter(movieList); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getBaseContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(mAdapter); mAdapter.notifyDataSetChanged(); ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) { @Override public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) { return false; } @Override public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int position) { for (int i = 0; i < array.size(); i++) { array.remove(i); } } }; new ItemTouchHelper(simpleCallback).attachToRecyclerView(recyclerView); Here is the function that fills the list:
public void prepareList() { for (int i = 0; i < array.size(); i++) { JsonObject object = array.get(i).getAsJsonObject(); Log.w("MY_TAG", String.valueOf(object.get("filename"))); Log.w("MY_TAG", String.valueOf(object.get("data"))); Integer data = object.get("data").toString().getBytes().length; movieList.add(new FileList(String.valueOf(object.get("filename")), String.valueOf(data))); } } Here, for example, I found such a solution, but after its introduction into the svayp listener, my application starts crashing for various reasons. I can not understand where and what I am doing wrong. I hope for your help and useful tips.
update
@Override public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int position) { for (int i = 0; i < array.size(); i++) { array.remove(i); // ΡΠ΄Π°Π»ΡΠ΅ΠΌ Π·Π°ΠΏΠΈΡΡ ΠΈΠ· ΠΈΡΡΠΎΡΠ½ΠΈΠΊΠ° (ΠΊΠ°ΠΊ Ρ ΠΏΠΎΠ½ΡΠ») mAdapter.notifyItemRemoved(position); // ΡΠ²Π΅Π΄ΠΎΠΌΠ»ΡΠ΅ΠΌ Π°Π΄Π°ΠΏΡΠ΅Ρ ΠΎΠ± ΡΠ΄Π°Π»Π΅Π½ΠΈΠΈ movieList.remove(position); // ΡΠ΄Π°Π»ΡΠ΅ΠΌ Π·Π°ΠΏΠΈΡΡ ΠΈΠ· ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ Π°Π΄Π°ΠΏΡΠ΅ΡΠ° ---- Π·Π΄Π΅ΡΡ ΠΎΡΠΈΠ±ΠΊΠ° } } error code:
java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
position) and when there are fewer elements in the list thanpositionand an error is fired. - woesssjava.lang.IndexOutOfBoundsException: Index: 4, Size: 4although everything is yours - Andrew Goroshko