2017-08-16 111 views
-1

我有一個應用程序,其中包含用於將數據從sqlite數據庫填充到recyclerview的適配器recyclerview。問題是,當我從RecyclerView刪除項目它不更新recyclerview。如何在android中更新recyclerview?

代碼: -

public void onClick(View v) { 
      String title; 
      title = modelList.get(position).getNotiifcationTitle(); 
      notificationDBHelper.removeSingleNotification(title); 
      modelList.remove(position); 
      notifyItemRemoved(position); 
      modelList.clear(); 
      //Updating ArrayList 
      modelList = notificationDBHelper.getAllNotificationsData(); 
      swapItems(modelList); 
      //Setting deleted notification count 
      notificationDBHelper.insertNotificationDeleteCount(1); 
     } 
+0

添加swapItems方法 –

+0

mAdapter.notifyDataSetChanged();或者在從數據庫再次加載數據之後執行刪除操作時,調用用於將數據附加到recyclerview的方法。 –

回答

0

使用adapter.notifyDataSetChanged();當你想更新你的recyclerview

0

更新您的適配器的最後這樣

public void onClick(View v) { 
     String title; 
     title = modelList.get(position).getNotiifcationTitle(); 
     notificationDBHelper.removeSingleNotification(title); 
     modelList.remove(position);    
     modelList.clear(); 
     //Updating ArrayList 
     modelList = notificationDBHelper.getAllNotificationsData(); 
     swapItems(modelList); 
     //Setting deleted notification count 
     notificationDBHelper.insertNotificationDeleteCount(1); 
     notifyItemRemoved(position); 
    }