【问题标题】:List not passing to the Recycler Adapter from Activity列表未从 Activity 传递给 Recycler Adapter
【发布时间】:2020-05-19 21:36:54
【问题描述】:

我正在尝试将 ArrayList 传递给我的回收器适配器类。我已经在我的代码中成功实现了 2 个其他回收器视图,但在这种情况下,正在传递等于 null 的数组列表。这是我的活动代码。

public class PlacedOrders extends AppCompatActivity {

private Toolbar tb;
private User u;
private ArrayList<OrderModel> orderList = new ArrayList<>();
private DatabaseReference root = FirebaseDatabase.getInstance().getReference();
private DatabaseReference ordersReference;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_placed_orders);

    tb = findViewById(R.id.customToolbar);
    setSupportActionBar(tb);

    Intent i = getIntent();
    u = i.getParcelableExtra("USER_DATA");

    ordersReference = root.child("Orders").child(u.getUserId());
    setRecyclerView();
}

public void setRecyclerView(){

    ordersReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()){
                OrderModel om = (ds.getValue(OrderModel.class));
                orderList.add(om);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    RecyclerView recyclerView = findViewById(R.id.recycler_view_storeOrders_parent);
    recyclerView.setHasFixedSize(false);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    PlacedOrderParentAdapter placedOrderParentAdapter = new PlacedOrderParentAdapter(this, orderList);
    recyclerView.setAdapter(placedOrderParentAdapter);

    }
}

我已经使用调试器检查了 for 循环,数组已成功填充循环中的项目,但一旦循环结束,它的大小就会降为零。 非常感谢您的帮助。

【问题讨论】:

  • 只是添加。当我在不使用数据快照循环的情况下手动将 OrderModel 对象添加到 ArrayList 时。回收站视图工作正常

标签: android firebase-realtime-database android-arrayadapter android-recyclerview


【解决方案1】:

在设置你的适配器之前,你可以试试这个:

public void setRecyclerView(){

RecyclerView recyclerView = findViewById(R.id.recycler_view_storeOrders_parent);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
PlacedOrderParentAdapter placedOrderParentAdapter = new PlacedOrderParentAdapter(this, orderList);
recyclerView.setAdapter(placedOrderParentAdapter);

ordersReference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()){
            OrderModel om = (ds.getValue(OrderModel.class));
            orderList.add(om);
        }
       placedOrderParentAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

}

填充orderList后调用notifyDataSetChanged()方法

【讨论】:

    【解决方案2】:

    这是因为recyclerview.setAdapter在从数据库中检索数据之前被调用并且onDataChange方法被调用。 在我看来处理这种情况的最佳方法是在你的适配器类中创建一个方法来获取新项目并在添加项目后在其中调用notifyDataSetChanged 以加载列表中的新更改
    在您的适配器类中:

    public void addItem(OrderModel order){
       myorderlist.addItem(order);
       notifyDataSetChanged();
    }
    

    然后你的代码就变成这样了

    public void setRecyclerView(){
    
    RecyclerView recyclerView = findViewById(R.id.recycler_view_storeOrders_parent);
    recyclerView.setHasFixedSize(false);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    PlacedOrderParentAdapter placedOrderParentAdapter = new PlacedOrderParentAdapter(this, orderList);
    recyclerView.setAdapter(placedOrderParentAdapter);
    
    ordersReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()){
                OrderModel om = (ds.getValue(OrderModel.class));
                placedOrderParentAdapter.addItem(om);
            }
    
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
    
        }
    });
    
    }
    
    

    【讨论】:

      【解决方案3】:

      这是因为onDataChange() 回调需要一些时间才能执行:

      你可以这样做:

      你在setRecyclerView() 方法中看到这两行吗:

      PlacedOrderParentAdapter placedOrderParentAdapter = new PlacedOrderParentAdapter(this, orderList);
      recyclerView.setAdapter(placedOrderParentAdapter);
      

      将它们直接移动到 onDataChange() 中的 for 循环下方:

      ...........
      ordersReference.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
              for(DataSnapshot ds : dataSnapshot.getChildren()){
                  OrderModel om = (ds.getValue(OrderModel.class));
                  orderList.add(om);
              }
           //here
           PlacedOrderParentAdapter placedOrderParentAdapter = newPlacedOrderParentAdapter(this, orderList);
           recyclerView.setAdapter(placedOrderParentAdapter);
          }
          ..........
          ..........
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-30
        相关资源
        最近更新 更多