首页 > 解决方案 > 从 firebase 加载图标并将其设置为底部导航栏的图标

问题描述

我正在尝试将图标动态加载到我的底部导航栏中。每次,用户单击一个新变量(在我的情况下是一个新城市),该变量的图标应从 Firebase 服务器下载并设置到底部导航栏。到目前为止,我的代码如下所示:

// first I load the relevant Icon from firebase
iconRef = mFirebaseDatabase.getReference().child(city);
    iconRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            cityIcon = (String) dataSnapshot.getValue();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

// this is for initiating bottom navigation bar
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
Menu menu = bottomNavigationView.getMenu();

// the following shall load the Id of the part in the bottom navigation bar, where the icon shall be passed to
ImageView iconView = findViewById(R.id.CityIcon);
Picasso.get().load(cityIcon).into(iconView);
menuItemCity.setTitle(cCity);
// the problem is, when I start the activity it crashes

或者,我认为这可以帮助我:

// but the following gives me an error as I am only allowed to enter an Integer and I am entering a String 
MenuItem menuItemCity = menu.getItem(0);
menuItemCity.setIcon(cityIcon); 

我也在考虑首先将所有图标保存到可绘制文件夹中。但是因为我希望能够在启动应用程序后更改图标,所以它不能满足我的要求。虽然可能有可能在从服务器下载图标后将它们保存到可绘制文件夹中 - 也许你们中的一些人对如何解决这个问题有任何想法。

标签: androidandroid-studioiconsbottomnavigationview

解决方案


推荐阅读