【问题标题】:Why Fragment onResume() not udating RecyclerView Correctly?为什么 Fragment onResume() 不能正确更新 RecyclerView?
【发布时间】:2016-10-15 07:18:59
【问题描述】:

在我的应用程序中,当下载时间从警报接收器服务到达时,我使用 AlarmManager 安排下载任务我下载文件并从自定义对象的 ArrayList 中删除,然后使用 sharedpref 保存它,如果它已经像这样运行,则在此启动 Activity 之后保存

if (mIsInForegroundMode == true) {
                            Intent intent1 = new Intent(this, DownloadingActivity.class);
                            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent1);

                        }

如果活动正在运行,那么Fragment onResume() 将在我获取保存列表和更新的地方被调用

@Override
public void onResume() {
    super.onResume();
    getDestroyedData();
    adapter.notifyDataSetChanged();

}  

public void getDestroyedData() {
    String getPendingDownloadsList = preferences.getString("plist", "");

    Type type2 = new TypeToken<List<DownloadingActivity.scheduleListType>>(){}.getType();

    if (!getPendingDownloadsList.equals("")) {
        pending=gson.fromJson(getPendingDownloadsList, type2);


    }
}  

现在,当片段的onResume 运行时,我成功获得了列表数据,但是当调用adapter.notifyDataSetChanged(); 时,数据在 RecyclerView 上更新,但它没有显示正确的值,但是当我重新启动活动时,RecyclerView 显示正确的值。我认为adapter.notifyDataSetChanged(); 无法正常工作。

这也是我在 onCreate() 中设置适配器的代码

adapter = new PendingAdapter(pending, this.getActivity());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this.getActivity()));  

更新
这是我的适配器

public class PendingAdapter extends RecyclerView.Adapter<PendingAdapter.PendingHolder> {

    ArrayList<DownloadingActivity.scheduleListType> pendingList;
    LayoutInflater inflater;

    PendingAdapter(ArrayList<DownloadingActivity.scheduleListType> pendingList, Context c) {

        this.pendingList = pendingList;
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public PendingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.custom_pending, parent, false);
        PendingHolder pendingHolder = new PendingHolder(v);
        return pendingHolder;
    }

    @Override
    public void onBindViewHolder(PendingHolder holder, int position) {

        holder.fileName.setText(pendingList.get(position).name);
        if (pendingList.get(position).reqCODE != 0) {
            holder.pendingType.setText("Start At " + pendingList.get(position).hour + ":" + pendingList.get(position).minute);
        }

    }

    @Override
    public int getItemCount() {
        return pendingList.size();
    }


     //also tried this function
   /** public void swap(ArrayList<DownloadingActivity.scheduleListType> newList) {
        if (pendingList != null) {
            pendingList.clear();
            pendingList.addAll(newList);
        } else {
            pendingList = newList;
        }
        notifyDataSetChanged();
    }**/

    public class PendingHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView fileName, pendingType;
        Button downloadByMobileBtn, removeFromPending;

        public PendingHolder(View itemView) {
            super(itemView);
            pendingType = (TextView) itemView.findViewById(R.id.pendingType);

            fileName = (TextView) itemView.findViewById(R.id.pendingFileNameTXT);
            downloadByMobileBtn = (Button) itemView.findViewById(R.id.downloadByMobileBTN);
            removeFromPending = (Button) itemView.findViewById(R.id.removeFromPending);

            downloadByMobileBtn.setOnClickListener(this);
            removeFromPending.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        ShowRemoveDialog(position);
                    }
                }
            });


        }

        @Override
        public void onClick(View v) {
            int p = getAdapterPosition();

            if(p!=RecyclerView.NO_POSITION) {
                if (v.getId() == downloadByMobileBtn.getId()) {


                    ShowDialog(p);
                }
            }
        }


    }

【问题讨论】:

    标签: android android-recyclerview notifydatasetchanged


    【解决方案1】:

    确保您传递给适配器的列表引用得到更新。 每次创建一个新列表是我的问题。 例如。

    ArrayList<String> myList = new ArrayList<String>();
    MyAdapter adapter = new MyAdapter(myList);
    myList = GetaNewList(); // Cause of error
    adapter.notifyDataSetChanged(); 
    

    相反,我传递了对我的列表的引用并更新了现有列表。

    GetUpdatedList(myList);
    

    函数内部-

    GetUpdatedList(ArrayList<String> myList)
    {
      myList.Append("foo");
    }
    

    【讨论】:

    • 我已经通过pending.clear()pending.addAll(tempPending) 尝试过,但是我在这里能做的都没有,请帮助
    【解决方案2】:

    似乎pending 是一个列表。那么你不应该重新分配这个列表,因为你到了这里新的指针和适配器仍将指向旧的。您应该清除列表,然后向其中添加项目。 像这样的:

    pending.clear();
    pending.addAll(gson.fromJson(getPendingDownloadsList, type2));
    

    【讨论】:

    • 通过removeFromPending 按钮@Stepan 删除行时,RecyclerView 也正确更新
    • 再次将更新列表更改为上述代码,这是正确的方法。并且不需要像ArrayList 这样写 - 到处写List 类型只有在创建时你可以写new ArrayList
    • ListArrayList 有什么区别
    • 没有什么能解决我的问题,请提供任何帮助
    猜你喜欢
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2021-12-11
    • 2019-11-11
    • 2013-11-01
    • 2018-08-25
    相关资源
    最近更新 更多