Android将字符串名称转换为R.drawable.name。

huangapple go评论88阅读模式
英文:

Android Cast String name to R.drawable.name

问题

在我的 Android 应用程序内部,我需要在名为 MyAdapter 的类中创建一个函数,该类在名为 MyActivity 的活动中使用(但不是 MainActivity)。该函数应具有以下结构:

public int getDrawableResourceId(String name) {
    // 简单的解释
    return R.drawable. + name;
}

例如,如果我调用 getDrawableResorceId("test"),它应该返回 R.drawable.test 的整数值。我不能使用 int drawableResourceId = getResources().getIdentifier(name, "drawable", getPackageName());,因为 Android Studio 告诉我无法解析方法 getResources()getPackageName()。如果我替换为:

int drawableResourceId = MyActivity.this
                                   .getResources()
                                   .getIdentifier(name, "drawable", MyActivity.this.getPackageName());

或者

int drawableResourceId = MainActivity.this
                                     .getResources()
                                     .getIdentifier(name, "drawable", MainActivity.this.getPackageName());

Android Studio 告诉我该活动不是封闭类。这就是为什么我直接想将字符串转换为 R.drawable.name

英文:

Inside my android application, I need to create a function inside a class MyAdapter which is used inside the Activity MyActivity (which is not the MainActivity). The function should have the following structure

public int getDrawableResourceId(String name) {
    // naive explanation
    return R.drawable. + name
}

For example if I call getDrawableResorceId("test") it should return the integer value of R.drawable.test. I cannot work with int drawableResourceId = getResources().getIdentifier(name, "drawable", getPackageName()); because Android Studio tells me that cannot resolve the methods getResources() and getPackageName(). If I replace it with

int drawableResourceId = MyActivity.this
                                   .getResources()
                                   .getIdentifier(name, "drawable", MyActivity.this.getPackageName());

or

int drawableResourceId = MainActivity.this
                                     .getResources()
                                     .getIdentifier(name, "drawable", MainActivity.this.getPackageName());

Android Studio tells me that the Activity is not an enclosing class. That is why I directly want to cast the String into R.drawable.name.

答案1

得分: 1

抱歉,我编辑了我的回答,把问题理解错了:

```java
public static int getDrawable(Context context, String name) {
   return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
}

更新 *

由于您在一个适配器中,假设您的类名是MyAdapter,请执行以下操作:

public class MyAdapter{

    private Context mContext;

    public MyAdapter(Context context){
        mContext = context;
    }
....

然后在下面您可以轻松调用:

getDrawable(mContext, "name")

<details>
<summary>英文:</summary>

Sorry I edited my answer, got the wrong question in mind:

public static int getDrawable(Context context, String name) {
return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
}


Update *

Since you are in an Adapter let&#39;s say you have MyAdapter as class name do:

public class MyAdapter{

private Context mContext;

public MyAdapter(Context context){
    mContext = context;
}

....


Then below you can easily call:

getDrawable(mContext, "name")


</details>



huangapple
  • 本文由 发表于 2020年4月4日 17:21:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61026062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定