android - 通过查询填充微调器
问题描述
我有一个有点奇怪/烦人的问题,我通过从 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);
}
解决方案
您正在进行异步调用以获取“Restaurant_Data”。因此,微调器仅在获取数据后才被填充。
您可以尝试通过禁用微调器来克服此问题(如您所建议的),直到成功检索数据。另一种解决方法是预取数据(如果您确定要打开此活动)。例如,您可以预取前面活动中的数据并将其存储在静态变量中。
推荐阅读
- c++ - 如何以体面的方式实现交互式旋转操作
- javascript - 获取动态年份范围并在选择列表中附加选项
- java - Spring Boot 2.3 中不打印自定义异常的 Stacktrace
- python - 如何遍历图形并返回最小路径?
- oracle - Oracle Apex - 隐藏静态内容区域
- sql - 从具有多个数字的消息中识别特定数字
- sql - SSRS 查询参数无法正常工作
- pthreads - pthread_attr_t 默认属性 - 官方文档
- ios - 无法使用 Transporter 将应用程序上传到 iTunesConnect
- amazon-web-services - 如何在使用打包程序构建 AWS AMI 时解决 spin-clouddriver-57bdcf65fd-dm5mq 和 spin-clouddriver-57bdcf65fd-fpqh9 错误