Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获取/处理具有多个条目的同步Firestore数据?_Android_Google Cloud Firestore - Fatal编程技术网

Android 如何获取/处理具有多个条目的同步Firestore数据?

Android 如何获取/处理具有多个条目的同步Firestore数据?,android,google-cloud-firestore,Android,Google Cloud Firestore,需要做的事情: 基本上,我希望Firestore=>集合“订单”=>每个文档上都有customerid和productid的文档=>onSuccess=>添加到OrderPOJOList=>调用getCustomerName(),然后getProductName()=>按顺序获取名称=>添加到相应的ArrayList=>最后合并三个ArrayList中的所有数据(OrderPOJOList、CustomerName、ProductName)到当前OrderPOJOList=>设置为适配器 问题:

需要做的事情:
基本上,我希望Firestore=>集合“订单”=>每个文档上都有customerid和productid的文档=>onSuccess=>添加到OrderPOJOList=>调用getCustomerName(),然后getProductName()=>按顺序获取名称=>添加到相应的ArrayList=>最后合并三个ArrayList中的所有数据(OrderPOJOList、CustomerName、ProductName)当前OrderPOJOList=>设置为适配器

问题: getCustomerName()和getProductName()中的两个侦听器异步运行,并将名称随机添加到arrayList…我只想以顺序显示适配器上的数据,但有时由于列表器异步运行,名称会在列表上交换

我应该怎么做才能从firestore中依次显示我的客户和产品名称

public class CurrentOrders extends AppCompatActivity {
    private List<CurrentOrdersPOJO> currentOrdersPOJOList;
    private List<OrderPOJO> orderPOJOList;
    private FirebaseFirestore firebaseFirestore;
    private String DocId, Area, cname, pname;
    private OrderPOJO orderPOJO;
    private CurrentOrdersPOJO currentOrdersPOJO;
    private int count = -1, count1 = -1, i;
    private RecyclerView recyclerView;
    private List<String> customerName, productName;

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

        //Current User Unique ID
        DocId = getIntent().getStringExtra("DocumentId");
        Area = getIntent().getStringExtra("Area");
        Log.w("ReachedCurrentOrders", "Doc Id: " + DocId + "\nArea: " + Area);

        currentOrdersPOJOList = new ArrayList<>();
        customerName = new ArrayList<String>();
        productName = new ArrayList<String>();
        orderPOJOList = new ArrayList<>();

        recyclerView = findViewById(R.id.activity_current_order_recyclerView);
        firebaseFirestore = FirebaseFirestore.getInstance();

        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

        firebaseFirestore.collection("order")
                .whereEqualTo("area", Area)
                .whereEqualTo("status", "active")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(final QuerySnapshot queryDocumentSnapshots) {
                        if (!queryDocumentSnapshots.isEmpty()) {

                            for (final QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {
                                count++;
                            }

                            for (final QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {

                                orderPOJO = queryDocumentSnapshot.toObject(OrderPOJO.class);
                                orderPOJOList.add(orderPOJO);

                                Log.d("Tagging", "The Customer UID: " + orderPOJO.getC_uid() + "\nThe Product Doc ID: " + orderPOJO.getP_docid());

                                count1++;

                                if (count == count1) {
                                    getCustomerName();
                                }

                            }//endof for loop

                        } else {
                            Toast.makeText(CurrentOrders.this, "No Orders in Your Area", Toast.LENGTH_SHORT).show();
                            Log.d("CurrentOrder", "Exception Here");
                        }

                    }

                });
    }//endofOnCreate

    public void getCustomerName() {
        count1 = -1;
        //Getting Customer Name from ID
        for (i = 0; i <= count; i++) {
            firebaseFirestore.collection("customer").document(orderPOJOList.get(i).getC_uid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()) {

                        cname = documentSnapshot.getString("name");
                        customerName.add(cname);

                        count1++;

                        if (count1 == count) {
                            getProductName();
                        }

                    } else {
                        Log.d("CurrentOrders", "Exception Here" + documentSnapshot.exists());
                    }
                }
            });
        }
    }//end of function

    public void getProductName() {
        count1 = -1;
        //Product Getting Name
        for (i = 0; i <= count; i++) {
            firebaseFirestore.collection("product").document(orderPOJOList.get(i).getP_docid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()) {
                        pname = documentSnapshot.getString("name");
                        productName.add(pname);

                        count1++;

                        if (count1 == count) {
                            callAdapter();
                        }
                    } else {
                        Log.d("CurrentOrders", "Exception Here" + documentSnapshot.exists());
                    }
                }

            });
        }
    }//endofFunction

    public void callAdapter() {
        for (int i = 0; i <= count; i++) {

            currentOrdersPOJO = new CurrentOrdersPOJO(customerName.get(i), orderPOJOList.get(i).getComplete_address(),
                    productName.get(i), orderPOJOList.get(i).getQuantity(), orderPOJOList.get(i).getStatus(), orderPOJOList.get(i).getArea(), orderPOJOList.get(i).getO_date());

            currentOrdersPOJOList.add(currentOrdersPOJO);
        }
        recyclerView.setAdapter(new CurrentOrdersAdapter(currentOrdersPOJOList, CurrentOrders.this));
    }//endofFunction

}//endofclass

另一个问题也提出了类似的问题,似乎您可以同步返回数据,因为方法本身是一个任务,您可以尝试使用Taks.await(任务)方法等待操作结束,这可能就是您要寻找的答案。

我通过使用上面提到的@Ricardo解决方案,并将该解决方案与使用Asynctask(后台线程)相结合,解决了这个问题,因为在主UI线程上调用Tasks.wait(task),它首先给出了IllegalStateException

因此,在AYSNTASK(后台线程)上使用:Tasks.await(task)

java.lang.IllegalStateException:不能在主应用程序线程上调用这是我使用Tasks.await(task)@Ricardo时得到的结果
  [1]: https://i.stack.imgur.com/X48JF.jpg