【问题标题】:Why my recyclerView doesn't display the new data after the adapter notify data is changed?为什么我的recyclerView在适配器通知数据更改后不显示新数据?
【发布时间】:2019-10-19 16:07:48
【问题描述】:

在父活动中,我在工具栏中有一个编辑文本,用户可以通过recyclerview显示的数据进行搜索。 当用户按下回车键时,edittext中的字符串通过以下方式发送到片段:

Bundle bundle = new Bundle();
                                    bundle.putString(predResult, placeid);
                                    MapFragment mapFragment = new MapFragment();
                                    ListRestFragment listRestFragment = new ListRestFragment();
                                    mapFragment.setArguments(bundle);
                                    listRestFragment.setArguments(bundle);
                                    getSupportFragmentManager().beginTransaction()
                                            .replace(R.id.map, mapFragment)
                                            .replace(R.id.list_frag, listRestFragment)
                                            .commit();

但是,不幸的是,当我的适配器被通知数据更改时,recyclerview 没有重新刷新,如下所示:

 private void queryToList(Query query) {

        query.addSnapshotListener((queryDocumentSnapshots, e) -> {
            restaurantList = queryDocumentSnapshots.toObjects(Restaurant.class);
            if (!myPrediction.contains("myPrediction")) {
                System.out.println(myPrediction);
                for (Restaurant item : restaurantList) {
                    if (item.getRestaurantID().contains(myPrediction)) {
                        restaurantListPred = new ArrayList<>();
                        restaurantListPred.add(item);
                        updateUI(restaurantListPred);
                    }
                }

            } else updateUI(restaurantList);
        });

    }

    private void updateUI(List<Restaurant> restaurants) {
        configureFab();
        configureRecyclerView(restaurants);
    }

    private void configureRecyclerView(List<Restaurant> restaurant) {

        this.adapter = new RestaurantAdapterClassic(restaurant);
        this.recyclerView.setAdapter(this.adapter);
        this.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(new SimpleItemDecorator(getContext()));
    }

当用户提出请求时,新的 List 会自动更新,但 recyclerView 不会显示新数据。

如果你想检查我的适配器实现:

public class RestaurantAdapterClassic extends RecyclerView.Adapter<RestaurantViewHolder> {

    private List<Restaurant> restaurantsList;

    // CONSTRUCTOR
    public RestaurantAdapterClassic(List<Restaurant> restaurants) {
        this.restaurantsList = restaurants;
    }

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

        // CREATE VIEW HOLDER AND INFLATING ITS XML LAYOUT
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.listview_pattern, parent, false);

        return new RestaurantViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RestaurantViewHolder viewHolder, int position) {
        viewHolder.updateWithRestaurant(this.restaurantsList.get(position));
    }

    // RETURN THE TOTAL COUNT OF ITEMS IN THE LIST
    @Override
    public int getItemCount() {
        return this.restaurantsList.size();
    }

    public Restaurant getRestaurant(int position) {
        return this.restaurantsList.get(position);
    }

    public void filterList(List<Restaurant> filteredList) {
        restaurantsList = filteredList;
        notifyDataSetChanged();
    }

}

我的错误或误解在哪里?

编辑解决方案 -

  1. 创建接口 实际上,通过监听器将新数据数据从我的 Parent Activity 发送到我的 Fragment 以观察数据何时发生变化。
  2. 保留发送到适配器的数据引用

实际上,我遇到的主要大问题是我的适配器在我发送新数组时没有刷新。原因是适配器使用列表/数组创建引用。如果你想刷新它,你需要通过获取列表来保留这个引用,删除它,然后通过方法 addALL 在里面放入/添加新数据。

【问题讨论】:

  • 我没有看到你在哪里调用filterList 方法
  • @DuyKhanhNguyen 在这个版本中,我没用过,我新建一个List来显示它
  • 如果你不使用它,那意味着notifyDataSetChanged() 永远不会被调用。如何将数据更新到视图中?
  • @DuyKhanhNguyen 正如他所说的 Md. Asaduzzaman :首先,adapter.notifyDataSetChanged();在代码中没有任何影响,因为在 updateUI 中,您每次调用它时都会创建适配器。
  • 那么你的问题是什么?第一时间不显示数据,还是数据变化时RecyclerView没有更新?

标签: android android-fragments adapter android-recyclerview


【解决方案1】:

首先,adapter.notifyDataSetChanged(); 对代码没有任何影响,因为在 updateUI 中,您每次调用它时都会创建适配器。

我认为,主要问题在这里:

if (item.getRestaurantID().contains(myPrediction)) {
    restaurantListPred = new ArrayList<>();
    restaurantListPred.add(item);
    updateUI(restaurantListPred);
}

这个块根本不执行。这就是列表没有更新的原因。

【讨论】:

  • 感谢您的发言。但实际上,这就是目的,如果用户提出请求,我只想要新列表中的一项。
  • 你是通过调试还是在updateUI里面放log来检查是否获取到更新RecyclerView的item?
  • 是的,在UpdateUI中,用户提出请求后,该项目是好的(我通过在其中放置日志进行了检查)
  • 请同时添加您的适配器实现
  • 在我睁开的眼睛里,我找不到任何问题来创建适配器。让我再次澄清您的问题,您的 RecyclerView 没有显示任何数据吗?还是没有更新
猜你喜欢
  • 1970-01-01
  • 2016-02-15
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2015-09-30
  • 1970-01-01
相关资源
最近更新 更多