如何在Android中动态引用R.drawable文件夹中的资源? [英] How to dynamically reference resources in the R.drawable folder in Android?

查看:78
本文介绍了如何在Android中动态引用R.drawable文件夹中的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的适配器有以下代码:

@Overridepublic void onBindViewHolder(GameViewHolder holder, int position) {final Games game = gameList.get(position);holder.awayTeamImageView.setBackgroundResource(R.drawable.fortyers);}

以上工作完美,但我对将显示的图像进行了硬编码.我真正需要的是从游戏列表中获取背景图像,我希望做这样的事情:

 holder.awayTeamImageView.setBackgroundResource(R.drawable.game.getaBackground());

<块引用>

但这会导致错误

如何动态设置imageView的背景资源?

更新:

我附上了所需效果的屏幕截图.

每周的时间表都会发生变化,因此列表总是会根据所选的星期而有所不同.

游戏构造函数:

公共游戏(DataSnapshot游戏){this.AwayTeam = game.child(AwayTeam").getValue().toString();this.AwayId = Integer.parseInt(game.child("AwayId").getValue().toString());this.HomeTeam = game.child(HomeTeam").getValue().toString();this.HomeId = Integer.parseInt(game.child("HomeId").getValue().toString());this.aBackground = game.child(aBackground").getValue().toString();this.hBackground = game.child(hBackground").getValue().toString();}

解决方案

另一种可能对您有帮助的方法是使用字符串中的 getIdentifier.为此,您将背景字符串名称与资源中的名称相同.例如,如果你想显示 ic_menu_camera 那么你的 aBackground 应该是相同的字符串.

示例游戏类:

公共类游戏{字符串 aBackground;公共游戏(字符串 aBackground){this.aBackground = aBackground;}公共字符串 getaBackground() {返回一个背景;}public void setaBackground(String aBackground) {this.aBackground = aBackground;}}

然后像这样使用它.阅读内嵌注释以详细了解.

//设置你想要的背景名称与你的drawable文件夹中的名称相同//没有任何扩展名 .png 或 .xml.只有名字应该在那里游戏游戏=新游戏(ic_menu_camera");ImageView imageView = findViewById(R.id.imageView);//使用这个通过名称获取id/* @param name 所需资源的名称.* @param defType 要查找的可选默认资源类型,如果type/";是* 未包含在名称中.可以为 null 以要求一个* 显式类型.* @param defPackage 要查找的可选默认包,如果package:";是* 未包含在名称中.可以为 null 以要求一个* 显式包.** @return int 关联的资源标识符.如果没有,则返回 0* 资源已找到.(0 不是有效的资源 ID.)* */int resId = getResources().getIdentifier(game.getaBackground(), drawable",getPackageName());//然后将该 id 设置为您的图像imageView.setBackgroundResource(resId);

I have the following code for my Adapter:

@Override
public void onBindViewHolder(GameViewHolder holder, int position) {
    final Games game = gameList.get(position);
    holder.awayTeamImageView.setBackgroundResource(R.drawable.fortyers);
}

The above works perfectly but I am hard coding the image that will be displayed. What I really need is to get the background image from the game list and I am looking to do something like this:

 holder.awayTeamImageView.setBackgroundResource(R.drawable.game.getaBackground());

But that causes an error

How can I dynamically set the imageView's background resource?

UPDATE:

I have attached a screenshot of the desired effect.

Each week the schedule will change so the list will always be different depending on the week selected.

Game constructor:

public Games(DataSnapshot game) {

    this.AwayTeam = game.child("AwayTeam").getValue().toString();
    this.AwayId = Integer.parseInt(game.child("AwayId").getValue().toString());

    this.HomeTeam = game.child("HomeTeam").getValue().toString();
    this.HomeId = Integer.parseInt(game.child("HomeId").getValue().toString());

    this.aBackground = game.child("aBackground").getValue().toString();
    this.hBackground = game.child("hBackground").getValue().toString();

}

解决方案

Another approach that might help you is using getIdentifier from the string. For this what you have make a background string name same as the one in your resource. e.g if you want to show ic_menu_camera then your aBackground should be the same string.

Example game class:

public class Game {

    String aBackground;

    public Game(String aBackground) {
        this.aBackground = aBackground;
    }

    public String getaBackground() {
        return aBackground;
    }

    public void setaBackground(String aBackground) {
        this.aBackground = aBackground;
    }
}

Then use it like this. Read inline comments to understand in detail.

//set the background name that you want same as name in your drawable folder
        // without any extension .png or .xml. Just name should be there
        Game game = new Game("ic_menu_camera");
        ImageView imageView = findViewById(R.id.imageView);
        //get the id by name using this
        /*  @param name The name of the desired resource.
             * @param defType Optional default resource type to find, if "type/" is
             *                not included in the name.  Can be null to require an
             *                explicit type.
             * @param defPackage Optional default package to find, if "package:" is
             *                   not included in the name.  Can be null to require an
             *                   explicit package.
             * 
             * @return int The associated resource identifier.  Returns 0 if no such
             *         resource was found.  (0 is not a valid resource ID.)
             * */
        int resId = getResources().getIdentifier(game.getaBackground(), "drawable",getPackageName());
        // then set that id to your image
        imageView.setBackgroundResource(resId);

这篇关于如何在Android中动态引用R.drawable文件夹中的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆