android - 如何刷新 TextView
问题描述
我有以下情况:
我有活动和几个片段。在 onCreate 方法的开头,我总是加载上图中的片段:
private void setInitialFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
DietDiaryFragment dietDiaryFragment = (DietDiaryFragment) fragmentManager.findFragmentByTag("DietDiaryFragment");
if (dietDiaryFragment == null) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
dietDiaryFragment = new DietDiaryFragment();
setArguments(dietDiaryFragment);
fragmentTransaction.add(R.id.content_main_screen, dietDiaryFragment,"DietDiaryFragment");
fragmentTransaction.commit();
}
}
其中 setArguments 方法如下:
private void setArguments(DietDiaryFragment dietDiaryFragment) {
Bundle bundle = new Bundle();
bundle.putParcelable("breakfast", breakfast);
bundle.putParcelable("lunch", lunch);
bundle.putParcelable("dinner", dinner);
bundle.putParcelable("snacks", snacks);
dietDiaryFragment.setArguments(bundle);
}
在活动中,我根据日期从 Firebase 下载给定膳食中的所有产品,并将其发送到上面显示的片段:
private void getProductsFromDatabaseLunch() {
lunch.getTotalProducts().clear();
lunch.getIdProducts().clear();
lunch.setTotalCalories(0);
firebaseFirestore.collection("Users").document(currentUserUID)
.collection("Types of Meals").document("Lunch")
.collection("Date of Lunch").document(date)
.collection("List of Products")
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
for(DocumentSnapshot documentSnapshot: task.getResult().getDocuments()) {
lunch.getIdProducts().add(documentSnapshot.getId());
lunch.getTotalProducts().add(documentSnapshot.toObject(Product.class));
}
lunch.calculateTotalCalories();
onSendTotalCaloriesLunch.sendTotalCaloriesLunch(lunch.getTotalCalories());
}
if(getFragmentRefreshLunchAdapter() != null) {
getFragmentRefreshLunchAdapter().refreshLunchAdapter();
}
}
});
}
从 Firebase 下载产品是异步的,所以第一个没有产品的空 ArrayLists 将被传递,只有在一段时间后产品被添加。在 RecyclerView 中显示产品没有问题,因为我使用接口刷新了 Adapter。
if(getFragmentRefreshLunchAdapter() != null) {
fragmentRefreshLunchAdapter.refreshLunchAdapter();
}
但是……剩下的呢?例如,在图中我标记了 TextView 负责给定膳食的总卡路里。在这里它将始终为零,因为如果一开始没有产品,则无法计算任何东西。我使用接口解决了这个问题:
onSendTotalCaloriesDinner.sendTotalCaloriesDinner(dinner.getTotalCalories());
其中方法如下:
((MainScreen)getActivity()).onSetSendTotalCaloriesLunch(new MainScreen.OnSendTotalCaloriesLunch() {
@Override
public void sendTotalCaloriesLunch(float calories) {
if(lunch.getTotalProducts().size() == 0) {
textViewTotalCaloriesLunch.setText("");
} else {
textViewTotalCaloriesLunch.setText(product.formatNumberWithoutDecimalPlaces(calories)); }
}
});
有没有其他方法可以解决这个问题?我是否必须通过接口单独发送所有值?当我尝试使用 BottomNavigationView 在片段之间导航时也会出现问题,然后也会出现零。什么,例如,如果我想创建一个图表,我是否还必须通过界面构建它?有什么优雅的解决方案吗?
解决方案
为什么不将 onChange 侦听器附加到您的 arrayList,这样每当您 firebase 返回值时,它将触发您的 arrayList 中的 onChange,您可以从那里更改您的 textView:
theList.addListener(new ListChangeListener<Item>() {
public void onChanged(Change<tem> c) {
}
});
推荐阅读
- c# - 表单提交 - 为每个字段添加额外的参数并在服务器上读取它们
- python-3.x - 如何从已经使用networkx创建的多个节点添加多个边?
- python - Airflow 1.10.2 未将日志写入 S3
- amazon-web-services - 如何在 Redshift(亚马逊)中更新 UDF?
- heroku - 无法在“main”中找到应用程序对象“app”-Heroku CLI
- r - 检查数据框中的特定行组中是否存在值
- r - 缓存矩阵的逆
- ios - 如何将从 ftp URL 检索到的数据转换为 Swift 中的可用对象
- java - 如何让我的回收器适配器检索发送到 firebase 数据库的消息?
- mips - 当我在 MIPS 中使用 sb 命令时,有没有办法确保我的字节正确加载?