android - 从 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);
我也在考虑首先将所有图标保存到可绘制文件夹中。但是因为我希望能够在启动应用程序后更改图标,所以它不能满足我的要求。虽然可能有可能在从服务器下载图标后将它们保存到可绘制文件夹中 - 也许你们中的一些人对如何解决这个问题有任何想法。
解决方案
推荐阅读
- javascript - 通过单击子区域中的按钮关闭模式内联对话框并防止它重新打开
- ios - Swift 地图注释
- python - 列出目录中的所有文件,然后使用命令行选择一个要使用的文件 - Python
- ios - iOS Metal 双缓冲 120 fps
- itext7 - iText7 中的 PdfPCellEvent 等效项是什么?
- vba - VBA 代码基于用户输入框应用过滤器,用于列字母数字和日期
- angularjs - 打开一个模态是返回结果而不是在关闭一个模态之后
- javascript - JS PayPal REST 错误:MALFORMED_REQUEST
- python - 如何通过处理错误来完成流程?
- c# - 如何在机器人框架直线中始终打开麦克风