title: Update RecyclerView with DiffUtil and AsyncListDiffer not Working
body: <p>I'm creating a <code>RecyclerView</code> in <code>Fragment</code>using <code>DiffUtil</code> and <code>AsyncListDiffer</code> because I don't want to use <code>notifyDataSetChanged()</code> method. But I'm facing a problem while using this approach. I'm trying to retrieve items from the database, but every time the data changes (increase) the UI won't update directly. I have to close my app first and then open it again or go to another class to check the updated item.</p>
  <p>This is my adapter:</p>
  <pre><code>public class AdapterSaham extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; implements SectionIndexer, SearchFilter {
     public static final int SECTION_VIEW = 0;
     public static final int CONTENT_VIEW = 1;
  
     List&lt;DaftarSaham&gt; daftarSahamList;
     private ArrayList&lt;Integer&gt; mSectionPositions;
     private static int  lastClicked = -1;
     WeakReference&lt;Context&gt; mContextWeakReference;
     private ItemClickListener itemClickListener;
     private int lastPosition = -1;
     int row_index = -1;
  
     public AdapterSaham(ArrayList&lt;DaftarSaham&gt; daftarSaham, Context context, ItemClickListener clickListener){
         this.daftarSahamList = daftarSaham;
         this.mContextWeakReference = new WeakReference&lt;Context&gt; ( context );
         this.itemClickListener = clickListener;
     }
  
     private static final DiffUtil.ItemCallback&lt;DaftarSaham&gt; DIFF_CALLBACK = new DiffUtil.ItemCallback&lt;DaftarSaham&gt; () {
         @Override
         public boolean areItemsTheSame(@NonNull DaftarSaham oldItem, @NonNull DaftarSaham newItem) {
             return newItem.getTicker ().equals ( oldItem.getTicker () ) &amp;&amp; newItem.getCompanyName ().equals ( newItem.getCompanyName () );
         }
  
         @SuppressLint(&quot;DiffUtilEquals&quot;)
         @Override
         public boolean areContentsTheSame(@NonNull DaftarSaham oldItem, @NonNull DaftarSaham newItem) {
             return newItem.equals ( oldItem );
         }
  
     };
  
     private  AsyncListDiffer&lt;DaftarSaham&gt; differ = new AsyncListDiffer&lt;DaftarSaham&gt; (this, DIFF_CALLBACK  );
  
     @NonNull
     @Override
     public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  
         Context ctx = mContextWeakReference.get ();
         View view = LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.list_item,parent,false  );
        
     
        return new ItemViewHolder ( view, ctx , itemClickListener);
  
     }
  
     @Override
     public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
         Context ctx = mContextWeakReference.get ();
         if (ctx == null){
             return;
         }
        
         ItemViewHolder itemViewHolder= (ItemViewHolder) holder;
         DaftarSaham daftarSaham =  differ.getCurrentList ().get ( position);
         itemViewHolder.txtTickerSaham.setText ( daftarSaham.getTicker () );
         itemViewHolder.txtCompanyName.setText ( daftarSaham.getCompanyName () );
     }
  
     public class ItemViewHolder extends RecyclerView.ViewHolder {
         private TextView txtTickerSaham, txtCompanyName;
         private LinearLayout cardView;
  
         ItemClickListener itemClickListener;
  
         public ItemViewHolder(@NonNull View itemView, final Context context, ItemClickListener itemClickListener) {
             super ( itemView );
             txtTickerSaham = (TextView) itemView.findViewById ( R.id.tickerSaham );
             txtCompanyName = (TextView) itemView.findViewById ( R.id.tv_company_name );
             cardView = (LinearLayout) itemView.findViewById ( R.id.cvRecycler );
  
             this.itemClickListener = itemClickListener;
  
         }
  
     }
  
    @Override
     public int getItemCount() {
  
         return differ.getCurrentList ().size ();
     }
  
     public void submitList(List&lt;DaftarSaham&gt; newdataList){
         differ.submitList ( newdataList );
         daftarSahamList = new ArrayList&lt;&gt; (newdataList);
     }
  
  </code></pre>
  <p>Code in Fragment:</p>
  <pre><code> ViewGroup.LayoutParams params=recyclerViewIndexer.getLayoutParams();
          params.height= h;
          recyclerViewIndexer.setLayoutParams(params);
          recyclerViewIndexer.setHasFixedSize ( true );
          recyclerViewIndexer.setLayoutManager ( new LinearLayoutManager ( getContext ()) );
  
   StringRequest stringRequest = new StringRequest ( Request.Method.GET, URL,
                 new Response.Listener&lt;String&gt; () {
                     @Override
                     public void onResponse(String response) {
                         ArrayList&lt;HashMap&lt;String,String&gt;&gt; list = new ArrayList&lt;&gt; ();
                         try {
                             JSONObject jsonObject = new JSONObject (response);
                             JSONArray jsonArray = jsonObject.getJSONArray ( &quot;data_emiten&quot; );
                             for(int i = 0; i &lt; jsonArray.length (); i++){
                                 JSONObject jo = jsonArray.getJSONObject ( i );
                                 String ticker = jo.getString ( &quot;ticker&quot; );
                                 String companyName = jo.getString ( &quot;companyName&quot; );
                                 daftarNonSection.add ( new DaftarSaham (ticker, companyName, false) );
  
                             }
  
                         } catch (JSONException e) {
                             e.printStackTrace ();
                         }
  
                         getHeaderListLetter ( daftarSahamList,daftarNonSection );
                         adapterSaham = new AdapterSaham ( daftarSahamList, getContext (), new AdapterSaham.ItemClickListener () {
                             @Override
                             public void onItemClick(Integer numbers, boolean isSelected) {
                                 String ticker = &quot;&quot;;
                                 if (isSelected ){
                                     ticker = daftarSahamList.get ( Integer.valueOf ( numbers) ).ticker;
                                 }
                                 SlidingTab slidingTab = (SlidingTab) getActivity ();
                                 slidingTab.setTicker (ticker);
                             }
                         } );
                         recyclerViewIndexer.setAdapter ( adapterSaham );
                         adapterSaham.submitList ( daftarSahamList );
                         Log.d ( &quot;TAG&quot;, &quot;onResponse: &quot;+ response );
                     }
                 },
                 new Response.ErrorListener () {
                     @Override
                     public void onErrorResponse(VolleyError error) {
                         Log.d ( &quot;TAG&quot;, &quot;onErrorResponse: &quot;+error );
                     }
                 } );
  
         int socketTimeOut = 50000;
         RetryPolicy policy = new DefaultRetryPolicy (socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
         stringRequest.setRetryPolicy ( policy );
         RequestQueue requestQueue = Volley.newRequestQueue ( getContext () );
         requestQueue.add ( stringRequest );
     }
  </code></pre>
  <p>My model:</p>
  <pre><code>public class DaftarSaham implements Comparable&lt;DaftarSaham&gt; {
  
      public String ticker;
      public String companyName;
      public boolean isSection;
      private boolean isSelected = false;
  
      public DaftarSaham() {
  
      }
  
      public DaftarSaham(String ticker, String companyName, boolean isSection) {
          this.ticker = ticker;
          this.companyName = companyName;
          this.isSection = isSection;
      }
  
      public String getCompanyName() {
          return companyName;
      }
      public void setTicker(String ticker) {
          this.ticker = ticker;
      }
  
      public void setCompanyName(String companyName) {
          this.companyName = companyName;
      }
  
  
      public String getTicker() {
          return ticker;
      }
   }
  
  </code></pre>
  <p>I don't know why it's not working because I have tried using <code>DiffUtil</code> and <code>AsyncListDiffer</code> before and there is no problem. Any helps or suggestions would be great. Thanks in advance.</p>