首页 > 解决方案 > 如何刷新 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 在片段之间导航时也会出现问题,然后也会出现零。什么,例如,如果我想创建一个图表,我是否还必须通过界面构建​​它?有什么优雅的解决方案吗?

标签: androidtextview

解决方案


为什么不将 onChange 侦听器附加到您的 arrayList,这样每当您 firebase 返回值时,它将触发您的 arrayList 中的 onChange,您可以从那里更改您的 textView:

theList.addListener(new ListChangeListener<Item>() {
     public void onChanged(Change<tem> c) {

         }
     });

推荐阅读