Android 如何刷新文本视图
我有以下情况: 我有一些活动和一些片段。在onCreate方法开始时,我总是加载上图中的片段:Android 如何刷新文本视图,android,textview,Android,Textview,我有以下情况: 我有一些活动和一些片段。在onCreate方法开始时,我总是加载上图中的片段: private void setInitialFragment() { FragmentManager fragmentManager = getSupportFragmentManager(); DietDiaryFragment dietDiaryFragment = (DietDiaryFragment) fragmentManager.findFragm
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);
}
((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)); }
}
});
在活动中,我从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();
}
}
});
}
但是。。。其余的呢?例如,在图中,我标记了TextView,它负责给定膳食的总热量。这里它总是零,因为如果一开始没有产品,就无法计算。
我使用接口解决了这个问题:
onSendTotalCaloriesDinner.sendTotalCaloriesDinner(dinner.getTotalCalories());
其中,方法如下:
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);
}
((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在片段之间导航时,问题也出现了,然后零也出现了。什么,例如,如果我想创建一个图表,我还需要通过接口构建它吗?
有什么优雅的解决方案吗?为什么不在arrayList中附加一个onChange侦听器,这样无论何时firebase返回值,它都会触发arrayList中的onChange,您可以从那里更改textView:
theList.addListener(new ListChangeListener<Item>() {
public void onChanged(Change<tem> c) {
}
});
theList.addListener(新的ListChangeListener(){
更改后的公共作废(更改c){
}
});