当前位置: 首页 > 知识库问答 >
问题:

回收器视图不更新notifyDataSetChanged上的内容

梁鸿风
2023-03-14

我正在使用Recycler视图显示元素列表。当我们单击每一行时,每一行上都有一个按钮,状态改变,背景颜色改变。状态更新后,我将调用notifyDataSetChanged(),但recyclerView不会刷新。

else if (allGoals.getStatus() == WorkoutCashConstants.GOALS_ACHIEVE) {
                        boolean networkStatus = checkNetworkStatus();
                        if (networkStatus) {
                            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            if (DATE_ACCESS == null) {
                                currentTimeStamp = dateFormat.format(new Date());
                            } else {
                                currentTimeStamp = DATE_ACCESS;
                            }
                            progDialog = ProgressDialog.show(CompanyGoals_AllGoals_Fragment.this.getActivity(), "", "Goal Progress Recorded");
                            progDialog.setCancelable(true);
                            AchieveGoals achieveGoals = new AchieveGoals(Integer.parseInt(allGoals.getGoalID()), currentTimeStamp);
                            Call<AchieveGoals> achieveGoalsCall = apiModule.achieveGoal(achieveGoals);
                            achieveGoalsCall.enqueue(new Callback<AchieveGoals>() {
                                @Override
                                public void onResponse(Response<AchieveGoals> response, Retrofit retrofit) {
                                    progDialog.dismiss();
                                    if (response.isSuccess()) {
                                        progDialog.dismiss();
                                        goalsAchieved = response.body();
                                        if (goalsAchieved.getAppStatusCode() == WorkoutCashConstants.SUCCESS_API) {
                                            realm = Realm.getInstance(getContext());
                                            realm.beginTransaction();
                                            AllGoalsDB goalsDB = realm.where(AllGoalsDB.class)
                                                    .equalTo("goalID", allGoals.getGoalID()).findFirst();

                                            CompeletedDatesDB compeletedDatesDB = realm.createObject(CompeletedDatesDB.class);
                                            compeletedDatesDB.setCompletedDate(currentTimeStamp);
                                            goalsDB.getCompeletedDatesDBs().add(compeletedDatesDB);
                                            realm.commitTransaction();


                                            UserInfoDB userInfoDB = realm.where(UserInfoDB.class)
                                                    .equalTo(WorkoutCashConstants.
                                                            COLUMN_USER_ID, userId)
                                                    .findFirst();

                                            if (userInfoDB != null) {
                                                realm.beginTransaction();
                                                userInfoDB.setSweatPoints(goalsAchieved.getData().getSweatPoints());
                                                realm.commitTransaction();
                                            }




                                            realm.close();



                                            notifyDataSetChanged();

                                        }
                                    }

共有1个答案

松钊
2023-03-14

NotefyDataSetChanged()检查getItemCount(),如果与适配器关联的列表中的项计数更改,则NotefyDataSetChanged()生效。在您的情况下,您可以在适配器中使用toggleSelect函数。我附加了我的一个示例适配器,这样,你就可以知道我们如何实现这种行为。

public class ToggleSelectionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private Cursor mCursor;
        private SparseBooleanArray selectedItems;

        public ToggleSelectionListAdapter(Cursor cursor) {
            mCursor = cursor;
            selectedItems = new SparseBooleanArray();
        }

        public void toggleSelection(int pos) {
            if (selectedItems.get(pos, false)) {
                selectedItems.delete(pos);
            } else {
                selectedItems.put(pos, true);
            }
            notifyItemChanged(pos);
        }

        public int getSelectedItemCount() {
            return selectedItems.size();
        }

        public void clearSelections() {
            selectedItems.clear();
            notifyDataSetChanged();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            public ViewHolder(final View itemView) {
                super(itemView);

                // Initialize your items of each row here
            }

            public void bindView(int pos) {
                try {
                    if (mCursor.isClosed())
                        return;

                    mCursor.moveToPosition(pos);

                    // Maintain a checked item list so that you can have a track if the item is clicked or not
                    if (checkedItems.contains(number) itemView.setBackgroundResource(R.drawable.background_selected);
                    else itemView.setBackgroundResource(R.drawable.background_normal);

                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                    if (checkedItems.contains(number)) {
                                        checkedItems.remove(number);
                                    } else {
                                        checkedItems.add(number);
                                    }

                                // Get the index of which item should toggle the background
                                int idx = mRecyclerView.getChildAdapterPosition(v);
                                toggleSelection(idx);
                            }
                        }
                    });
            }
        }


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View v;

            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row, parent, false);

            ViewHolder vh = new ViewHolder(v);

            return vh;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            if (holder instanceof ViewHolder) {
                ViewHolder vh = (ViewHolder) holder;

                vh.bindView(position);

            }
        }

        @Override
        public int getItemCount() {

            if (mCursor == null) {
                return 0;
            }

            int n = mCursor.getCount();
            return n;
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }

        synchronized public void swapCursor(Cursor cursor) {
            mCursor = cursor;
            notifyDataSetChanged();
        }
    }
 类似资料:
  • 我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我

  • 在其他回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图滚动正常,但内部回收器视图滚动不正常。 这是代码: ViewAdapter如下: 我尝试了以下两种回收商观点,但都无法解决问题 也尝试了这个:

  • 我进行一个API调用,得到一个响应,并将其存储在中。我有1个和1个要填充。现在,当我得到响应时,它是根据JSON(即顺序)的,但当我尝试在适配器中设置它时,它是随机的(即没有顺序) 按顺序如下: 现在,在这个适配器中,ImagePath不是按照以下顺序:

  • 我正在使用SQLite数据库填充RecyclerView。我想让RecyclerView实时更新。但我失败了。我尝试过使用“notifyDataSetChanged()”但它不起作用。 你能不能也告诉我如何通过制作一个“新适配器”来刷新回收器视图。 哪个是更好的通知DataSetChanged();或任何其他? 这是我的回收水 **编辑: adapter.update数据(列表); 在“locat

  • 您好,我目前正在使用Recyclerview ListAdapter,我想知道什么是等价的通知DataSetChanged或如何在添加值/记录后更新整个列表。? 我目前使用过此方法,但如果不刷新,则不会更新。 我想回到回收器视图。适配器和通知数据设置更改()。这样我的问题就解决了,你觉得怎么样?提前谢谢。

  • 我在里面使用了。我还将设置为false for 支持较低的API