简体   繁体   English

notifyDataSetChanged在recyclerView中不起作用

[英]notifyDataSetChanged doesn't work in recyclerView

I am using recyclerView whit adapter. 我正在使用recyclerView惠特适配器。 all work good, but when i am trying to change some data (mark item as liked), but notifyDataSetChanged doesnt update it in real time. 一切正常,但是当我尝试更改一些数据(标记为喜欢的项目)时,notifyDataSetChanged不会实时更新。 if i exit from activity and than start it again - all data save and correct. 如果我退出活动,然后重新开始-所有数据都会保存并更正。

here is my code: 这是我的代码:

@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();
    }

EDIT here is my Adapter: 在这里编辑是我的适配器:

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);
    }
    }

How did you implement your mContactAdapter ? 您是如何实现mContactAdapter Is it a subclass of RecyclerView.Adapter ? 它是RecyclerView.Adapter的子类吗?

You can use following methods to update the RecyclerView after data set changed. 更改数据集后,可以使用以下方法更新RecyclerView

    notifyItemInserted(n);
    notifyItemRemoved(n);
    notifyItemChanged(n);

How are you read data from your database to your adatper? 您如何从数据库读取数据到adatper? If you read it to List or something like that and use it when you call adapter, then you should, after add your data to database, afresh fill your List and then call notifyDataSetChange(); 如果您将其读到List或类似的东西并在调用适配器时使用它,则应在将数据添加到数据库后重新填充List,然后调用notifyDataSetChange();。

I guess: 我猜:

You get datas from ContentResolver . 您可以从ContentResolver获取数据。

Then you update ContentResolver . 然后,您更新ContentResolver

Now ContentResolver has changed but datas hasn't changed. 现在, ContentResolver已更改,但数据未更改。

Try reget dats from ContentResolver or just datas.get(index).change. 尝试从ContentResolver或仅从datas.get(index).change中获取数据。

Then notifyDataSetChange() . 然后notifyDataSetChange()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM