RecyclerView尚未显示列表,但数据正在被送入适配器。

问题描述 投票:0回答:1

我的数据来自 FireStore 到一个自定义类对象中,当我用一个 Toast 在for循环中,我可以显示所有我想要的数据,但我传递给适配器的列表是空的。

我试着找出它是空的原因,但没有任何效果。

这是我的主类,我的适配器类是这样的:

products.whereEqualTo("IDliv","kITQ8wiPshsnWqHDlP5D").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
            assert documentSnapshots != null;
            for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
            generalObject obj=document.getDocument().toObject(generalObject.class);
            for(int i=0;i<obj.getproducts().size();i++){
                String[] body;
                  body=obj.getproducts().get(i).toString().split("\\+");
                ProductModel p = new ProductModel(body[0].toString(), body[1].toString(), body[2].toString());
                prod.add(p);

            }
            }

    }
    });
if(prod.size()>0) {
    adapter = new ProductAdapter(prod);
    RecyclerView recview = findViewById(R.id.items_recycler_view);
    recview.setHasFixedSize(true);
    recview.setLayoutManager(new LinearLayoutManager(this));
    recview.setAdapter(adapter);
}else{
    Toast.makeText(getApplicationContext(),"this list is empty",Toast.LENGTH_LONG).show();
}
    }

而我的适配器类是这样的:

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

    private ArrayList<ProductModel> products;

    public ProductAdapter(ArrayList<ProductModel> product) {
        this.products = product;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = (View) LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_product, parent, false);

        return new ViewHolder(v);
    }

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

        ProductModel prod = products.get(position);

        holder.name.setText(prod.getGetProductName());
        holder.description.setText(prod.getProductMarque());
        holder.qty.setText(prod.getProductQte());


    }

    @Override
    public int getItemCount() {
        if (products != null) {
            return products.size();
        } else {
            return 0;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final View view;
        public final TextView name;
        public final TextView description;
        public final TextView qty;

        public ViewHolder(View view) {
            super(view);
            this.view = view;
            name = view.findViewById(R.id.productsName);
            description = view.findViewById(R.id.marque);
            qty=view.findViewById(R.id.quantity);


        }
    }
}

你知道是什么问题吗?

android arraylist android-recyclerview google-cloud-firestore adapter
1个回答
1
投票

你需要添加 notifyDataSetChanged() 时,事件回调结束。

    @Override
    public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
        assert documentSnapshots != null;
        for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
        generalObject obj=document.getDocument().toObject(generalObject.class);
        for(int i=0;i<obj.getproducts().size();i++){
            String[] body;
              body=obj.getproducts().get(i).toString().split("\\+");
            ProductModel p = new ProductModel(body[0].toString(), body[1].toString(), body[2].toString());
            prod.add(p);

        }
        adapter.notifyDataSetChanged(); // <<<<<< here is the change
   }

另一件事: if(prod.size()>0) 条件太早了,因为它不会匹配,因为数据是在后台线程上接收的,所以这个条件永远不会满足。把这部分改成。

adapter = new ProductAdapter(prod);
RecyclerView recview = findViewById(R.id.items_recycler_view);
recview.setHasFixedSize(true);
recview.setLayoutManager(new LinearLayoutManager(this));
recview.setAdapter(adapter);

if (prod.size()==0) {
    Toast.makeText(getApplicationContext(),"this list is empty",Toast.LENGTH_LONG).show();
}

0
投票

你从Firebase发出的查询是异步进行的 所以在你实例化适配器后,查询数据就会回来并被添加到产品列表中了

你的if语句没有被满足,因为产品列表实际上是空的,因为那段代码在查询返回之前被执行了。

// This is false here because this block is getting executed before 
// the Firebase query returns the data. However your not seeing the 
// toast, because you are passing the wrong context. Toast takes 
// an activity context, not application context.

if(prod.size()>0) {
    adapter = new ProductAdapter(prod);
    RecyclerView recview = findViewById(R.id.items_recycler_view);
    recview.setHasFixedSize(true);
    recview.setLayoutManager(new LinearLayoutManager(this));
    recview.setAdapter(adapter);
}else{
    Toast.makeText(getApplicationContext(),"this list is empty",Toast.LENGTH_LONG).show();
}

试着修改一下你的代码来创建适配器,然后在查询返回后再添加数据。

adapter = new ProductAdapter();
    RecyclerView recview = findViewById(R.id.items_recycler_view);
    recview.setHasFixedSize(true);
    recview.setLayoutManager(new LinearLayoutManager(this));
    recview.setAdapter(adapter);


products.whereEqualTo("IDliv","kITQ8wiPshsnWqHDlP5D").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
            assert documentSnapshots != null;
            for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
            generalObject obj=document.getDocument().toObject(generalObject.class);

            for(int i=0;i<obj.getproducts().size();i++){
                String[] body;
                  body=obj.getproducts().get(i).toString().split("\\+");
                ProductModel p = new ProductModel(body[0].toString(), body[1].toString(), body[2].toString());
                prodList.add(p);

            }

            // Tell the adapter we have new data that we want to add
            adapter.addNewData(prodList)

            }

    }
    });

然后稍微修改一下你的适配器

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

    private ArrayList<ProductModel> products;

    public ProductAdapter() {
        this.products = new ArrayList<ProductModel>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = (View) LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_product, parent, false);

        return new ViewHolder(v);
    }

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

        ProductModel prod = products.get(position);

        holder.name.setText(prod.getGetProductName());
        holder.description.setText(prod.getProductMarque());
        holder.qty.setText(prod.getProductQte());


    }

    @Override
    public int getItemCount() {
        if (products != null) {
            return products.size();
        } else {
            return 0;
        }
    }

    public void addNewData(data: List<ProductModel>) {
        this.products.clear();
        this.products.addAll(data);
        notifyDatasetChanged(); //This will tell the adapter we have new data and to re-inflate the views
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.