title: After drag and drop Recyclerview item, how can I provide the updated Position of a list item at onbind method of an adapter
body: <p>I have an application in which I fetching data from a database to <code>Recyclerview</code>. I am changing the recyclerview item's position by drag and drop, and at time of drag and drop I am getting an updated <code>ArrayList</code> and inserting into <code>Sqlite</code>.</p>
  
  <p>I also delete item on left swap. The problem occurs when I delete item after drag and drop. After drag/drop, the item's new position is not getting at delete function. You will understand this problem better after going through the code.</p>
  
  <p><strong>Code of Adapter's onbind</strong>: I am posting only problem related code</p>
  
  <pre><code>  @Override
      public void onBindViewHolder(@NonNull final MyContactHolder myContactHolder, final int i) {
  
  
          Log.e("poistionINAdapter:", String.valueOf(i));
  
          myContactHolder.DeleteText.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  removeflag=true;
                  customDataListModels.notify();
                  customDataListModels.remove(i);
                  notifyItemRemoved(i);
                  notifyItemChanged(i, customDataListModels.size());
                  Log.e("removeItem", String.valueOf(customDataListModels.toString()));
                  reCustom=customDataListModels;
  
                  if (removeflag == true) {
                      tempSqliteDatabaseHelper.deleteall();
                      for (int i = 0; i &lt; reCustom.size(); i++) {
                          Log.e("chname", String.valueOf(reCustom.get(i).getName()));
                          tempinsert(reCustom.get(i).getName(), reCustom.get(i).getNumber(), reCustom.get(i).getColor(), reCustom.get(i).getFSize(), 1);
                      }
                  }
              }
          });
  }
  </code></pre>
  
  <p><strong>Code at drag and drop stuff:</strong> this is also inside the adapter</p>
  
  <pre><code> @Override
      public boolean onItemMove(int fromPosition, int toPosition) {
          if (fromPosition &lt; toPosition) {
              for (int i = fromPosition; i &lt; toPosition; i++) {
                  Collections.swap(customDataListModels, i, i + 1);
  
              }
  //            Log.e("UpTODown", String.valueOf(toPosition));
          } else {
              for (int i = fromPosition; i &gt; toPosition; i--) {
                  Collections.swap(customDataListModels, i, i - 1);
  
              }
  //            Log.e("DownToUp", String.valueOf(toPosition));
          }
  
              Log.e("postion", "from pos : "+String.valueOf(fromPosition)+" : to Position :"+String.valueOf(toPosition));
  
          mListChangedListener.onNoteListChanged(customDataListModels);
  
          notifyItemMoved(fromPosition, toPosition);
  
          chCustom=customDataListModels;
          if (swapChecker == true) {
              tempSqliteDatabaseHelper.deleteall();
              for (int i = 0; i &lt; chCustom.size(); i++) {
                  Log.e("PositionInSwap", String.valueOf(chCustom.get(i).getName()));
                  tempinsert(chCustom.get(i).getName(), chCustom.get(i).getNumber(), chCustom.get(i).getColor(), chCustom.get(i).getFSize(), 1);
                  customDataListModels=chCustom;
              }
          }
          return true;
      }
  </code></pre>
  
  <p>As you can see, at drag and drop I am deleting the whole table of <code>sqlite</code> and inserting a new updated <code>arraylist</code>, at the same time as deleting the item.</p>
  
  <p>As I mentioned, the problem occurs after drag and drop. When I delete after drag and drop, it's deleting the old position item. So how can I provide a new position after drag and drop at onbind? </p>
  
  <p>If you require any more code or screen shots, ask me and I will post them.</p>