首页 > 解决方案 > 通过查询填充微调器

问题描述

我有一个有点奇怪/烦人的问题,我通过从 Firestore 查询必要的字符串元素来填充微调器下拉元素。查询部分不是问题。它是人口稠密的部分。微调器(实际上是 MaterialBetterSpinner)位于片段中。我必须等待一两秒钟才能打开微调器。如果我不这样做,那么微调器就不起作用。我尝试将查询移动到 onCreate 然后到片段的 onStart 覆盖无济于事。在不破坏用户体验的情况下,我有什么选择可能会使用处理程序禁用微调器几秒钟?

以下是方法:

@Override
    public void onStart() {
        super.onStart();
        FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
        Query myRestaurants = firebaseFirestore.collection("Restaurant_Data").whereArrayContains("users", getUser());
        myRestaurants.get().addOnSuccessListener(queryDocumentSnapshots -> {
            for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                Restaurant restaurant = documentSnapshot.toObject(Restaurant.class);
                spinnerList.add(restaurant.getRestaurantAddress());
                restaurantArrayList.add(restaurant);
            }
        }).addOnFailureListener(e -> {
            Log.d(TAG, "Exception for spinner: " + e.getMessage());
        });
    }

这个方法是从片段中的 onCreateView 调用的:

private void getRestaurants() {
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(restaurantContext, android.R.layout.simple_dropdown_item_1line, spinnerList);
        materialDesignSpinner = restaurantView.findViewById(R.id.restaurant_spinner);
        materialDesignSpinner.setAdapter(arrayAdapter);
    }

标签: androidandroid-fragmentsandroid-spinner

解决方案


您正在进行异步调用以获取“Restaurant_Data”。因此,微调器仅在获取数据后才被填充。

您可以尝试通过禁用微调器来克服此问题(如您所建议的),直到成功检索数据。另一种解决方法是预取数据(如果您确定要打开此活动)。例如,您可以预取前面活动中的数据并将其存储在静态变量中。


推荐阅读