2015-11-09 75 views
0

我正在使用recyclerView惠普適配器。所有工作都很好,但是當我嘗試更改某些數據(標記爲喜歡的項目)時,但notifyDataSetChanged不會實時更新它。如果我退出活動並再次啓動它 - 所有數據保存並更正。notifyDataSetChanged在回收站中不起作用查看

這裏是我的代碼:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_contacts, container, false); 

     initLoader(); 
     setAdapter(); 
} 

public void setAdapter(){ 

    mContactAdapter = new ContactsAdapter(getActivity().getApplicationContext(), this); 
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    mRecyclerView.setAdapter(mContactAdapter); 
} 

@Override 
    public void onFavoriteItemClicked(ImageView view, int position) { 
     mMainCursor.moveToPosition(position); 

     int status = -1; 
     int currentStatus = mMainCursor.getInt(ContactsActivityFragment.COL_CONTACT_FAVORITE); 
     switch (currentStatus){ 
      case SU_VARIABLES.CONTACT_IS_FAVORITE: 
       view.setImageResource(R.drawable.icon_fav); 
       status = SU_VARIABLES.CONTACT_NOT_FAVORITE; 
       break; 
      case SU_VARIABLES.CONTACT_NOT_FAVORITE: 
       view.setImageResource(R.drawable.icon_fav_active); 
       status = SU_VARIABLES.CONTACT_IS_FAVORITE; 
       break; 
     } 

     ContentValues values = new ContentValues(); 
     values.put(SU_Contract.ContactsEntry.CONTACT_FAVORITE, status); 
     String id = mMainCursor.getString(ContactsActivityFragment.COL_CONTACT_ID); 
     String selection = SU_Contract.ContactsEntry.CONTACT_ID + " = " + id; 

     getActivity().getContentResolver().update(SU_Contract.ContactsEntry.CONTENT_URI,values, selection, null); 
     mContactAdapter.notifyDataSetChanged(); 
    } 

編輯 這裏是我的適配器:

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder> { 

    public ContactsAdapter(Context c, OnContactItemClicked clicked){ 
     sContext = c; 
     sInterface = clicked; 
    } 

    private static OnContactItemClicked sInterface; 
    private static Context sContext; 
    private static Cursor sCursor; 
    private static ContactsViewHolder sHolder; 

    @Override 
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_people, parent, false); 
     sHolder = new ContactsViewHolder(rootView); 

     return sHolder; 
    } 

    @Override 
    public void onBindViewHolder(ContactsViewHolder holder, int position) { 
     if (sCursor != null){ 
      sCursor.moveToPosition(position); 
      String name = sCursor.getString(ContactsActivityFragment.COL_CONTACT_NAME); 
      String phone = sCursor.getString(ContactsActivityFragment.COL_CONTACT_PHONE); 
      String imgUrl = sCursor.getString(ContactsActivityFragment.COL_CONTACT_IMAGE); 
      int currentStatus = sCursor.getInt(ContactsActivityFragment.COL_CONTACT_FAVORITE); 
      switch (currentStatus){ 
       case SU_VARIABLES.CONTACT_NOT_FAVORITE: 
        sHolder.mUserRating.setImageResource(R.drawable.icon_fav); 
        break; 
       case SU_VARIABLES.CONTACT_IS_FAVORITE: 
        sHolder.mUserRating.setImageResource(R.drawable.icon_fav_active); 
        break; 
      } 
      holder.mUserName.setText(name); 
      if (phone != null && phone.length() > 9){ 
       holder.mUserPhone.setText(phone); 
      } 
     } 
    } 

    @Override 
    public int getItemCount() { 
     int size = 0; 
     if (sCursor != null){ 
      size = sCursor.getCount(); 
     } 
     return size; 
    } 

    public void swapCursor(Cursor cursor){ 
     if (sCursor != null && sCursor.isClosed()){ 
      sCursor.close(); 
     } 
     sCursor = cursor; 
     notifyDataSetChanged(); 
    } 
public static class ContactsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 

     @Bind(R.id.tv_profile_name) TextView mUserName; 
     @Bind(R.id.tv_contactItem_phone) TextView mUserPhone; 
     @Bind(R.id.img_user_card) ImageView mCardImg; 
     @Bind(R.id.img_user_phone) ImageView mPhoneImg; 
     @Bind(R.id.img_profile_rating) ImageView mUserRating; 
     @Bind(R.id.img_profile_photo) CircularImageView mUserPhoto; 



     public ContactsViewHolder(View itemView) { 
      super(itemView); 
      ButterKnife.bind(this, itemView); 
      itemView.setOnClickListener(this); 

     } 

     @OnClick(R.id.img_profile_rating) 
     public void onFavoriteClicked(){ 
      sInterface.onFavoriteItemClicked(sHolder.mUserRating, getAdapterPosition()); 
     } 


     @Override 
     public void onClick(View v) { 
      int pos = getAdapterPosition(); 
      sInterface.onRecyclerViewItemClicked(v, pos); 
     } 



    } 
    public interface OnContactItemClicked{ 
     void onRecyclerViewItemClicked(View view, int position); 
     void onFavoriteItemClicked(ImageView view, int position); 
    } 
    } 
+0

看看其中的一些問題提供了一個有用的答案給你: - http://stackoverflow.com/questions/24740557/notifydatasetchanged-not-working-on-recyclerview - http://stackoverflow.com/questions/24495542/notifydatasetchange-not-working-on-recyclerview - http://stackoverflow.com/questions/28225563/recyclerview-notifydatasetchanged-not-working-after-parse-query - http://stackoverflow.com/questions/27714127/recyclerview-adapter-notifydatasetchanged-not-working – androidevil

+0

nope,沒有人幫忙 –

+0

沒有足夠的信息來回答你的問題。 1.您的提供者實現是否通知對數據的更改? 2.你如何處理加載器回調(向我們展示)?另外,如果要從加載器回調中更新適配器,則無需在點擊監聽器中使用「mContactAdapter.notifyDataSetChanged();」。 – Ari

回答

0

你是如何實現你的mContactAdapter?它是RecyclerView.Adapter的子類嗎?

更改數據集後,您可以使用以下方法更新RecyclerView

notifyItemInserted(n); 
    notifyItemRemoved(n); 
    notifyItemChanged(n); 
+0

我更新我的問題併發布我的適配器。請看看 –

+0

@StanMalcolm你忘了在'OnFavouriteItemClick'之後交換光標,我沒有看到任何代碼要做。 – alijandro

0

您如何從數據庫中讀取數據到您的adatper?如果你將它讀入List或類似的東西,並在調用適配器時使用它,那麼在將數據添加到數據庫之後,應重新填充List,然後調用notifyDataSetChange();

0

我想:

你從ContentResolver得到DATAS。

然後你更新ContentResolver

現在ContentResolver已經改變,但數據沒有改變。

嘗試從ContentResolver或只是datas.get(index).change的reget dat。

然後notifyDataSetChange()