Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将包含ID的字符串转换为整数ID_Android_String - Fatal编程技术网

Android 将包含ID的字符串转换为整数ID

Android 将包含ID的字符串转换为整数ID,android,string,Android,String,我有一个简短的问题: 如何转换包含可绘制文件Id的字符串,即 String idString = "R.drawable.bubblegum"; 到一个整数 idInt 这样我就可以使用该ID来引用图像(,在SimpleAdapter中使用) 举个例子,我不能这么做: bubble.setImageDrawable(this.getResources().getDrawable(idString)); //not possible, cause idString is a String an

我有一个简短的问题:

如何转换包含可绘制文件Id的字符串,即

String idString = "R.drawable.bubblegum";
到一个整数

idInt
这样我就可以使用该ID来引用图像(,在SimpleAdapter中使用)

举个例子,我不能这么做:

bubble.setImageDrawable(this.getResources().getDrawable(idString));
//not possible, cause idString is a String an not an Id/Int
所以,我有一个包含id的字符串,但不幸的是,它是一个字符串


谢谢

int idInt=R.drawable.bubblegum


除非我在这里遗漏了什么。

如果您的
idString
是常量,即它在运行时不会更改,请遵循DeeV的答案。 如果它发生了变化,您可以查看方法。

在通过
getResources()
获得的
Resources
对象上调用
getIdentifier()
,如这些StackOverflow问题所示:


至少,我找不到解决这个问题的办法。似乎没有办法将字符串“R.id.mytext”转换成可以在findViewById(R.id.mytext)中使用的像R.id.mytext这样的整数。

虽然这个问题已经很老了,但缺少的是“id”和“drawable”是不同的资源类型。所以不是

getResources().getIdentifier(stringId, "id", "my.Package");
它是

您还可以使用活动上下文获取包名,如
activityContext.getPackageName()


你可以试试下面的方法

int id = getResources().getIdentifier("arr_name"+positionSelected,
                        "array", rootview.getContext().getPackageName());
我在微调器下拉列表中使用,获取数组字符串跟随父微调器
我可以帮你

您是否正在尝试将“R.drawable.bubblegum”转换为int?这是不可能的。@flash可以使用
getIdentifier
@flash:是的,我正在尝试。findViewById(R.drawable.bubblegum)如何返回ImageView?它传递对它的引用<代码>ImageView im=(ImageView)findViewById(R.id.bubblegum)
findViewById
仅接受ViewGroup层次结构中的ID号。您无法使用可绘制id。您忽略了该id仅为字符串的事实。抱歉,我犯了一个错误,我不想引用该ImageView,我只是尝试制作一个示例。这是错误的。但我编辑了它。我的问题是,就像njzk2所说的,id只被称为字符串;long intId=getResources().getIdentifier(stringId,“id”,“my.Package”);Log.d(“Test”,intId+);然后LogCat显示0。错了吗?我也有同样的问题如果你有任何解决办法请告诉我。谢谢回答的人没有抓住要点。假设您只是试图在编写代码时引用已经存在的资源的id。但这里只是一个想法,如果你根本不知道怎么办。或者您正试图分配一个id,例如Menuitem.add(int resId)???那样的话怎么办?
/**
 * Returns Identifier of String into it's ID as defined in R.java file.
 * @param pContext
 * @param pString defnied in Strings.xml resource name e.g: action_item_help
 * @return
 */
public static int getStringIdentifier(Context pContext, String pString){
    return pContext.getResources().getIdentifier(pString, "string", pContext.getPackageName());
}
int id = getResources().getIdentifier("arr_name"+positionSelected,
                        "array", rootview.getContext().getPackageName());