如何读取环路原料资源很多文件?环路、原料、文件、资源

2023-09-07 08:54:06 作者:寡虎难敌众狼

我有很多的文本文件,并希望把它在for循环!我得到有从去年的活动资源名的额外(S)并具有具有资源名称的数组从原材料的资源我的文本文件来自启动{D0,D1,D2,D3,...,D79}我要检查姓名(或名称)和数组名,并把查找名称资源​​!我有错误(RES = R.raw。(D [I]))我的code:

 中期业绩= 0;    的for(int i = 0; I< = 79; i = i + 1){        如果(s.equals(四[I])){            RES = R.raw。(D [I])        }    }    的InputStream = getResources()openRawResource(RES)。 

解决方案

您可以使用则getIdentifier(字符串名称,字符串DEFTYPE,字符串defPackage)动态获取资源ID,

 的ArrayList<整数GT; ID =新的ArrayList<整数GT;();的for(int i = 0; I< = 79;我++){  id.add(getResources()则getIdentifier(D+ I,原始,getPackageName()));} 
文件服务器资源管理器如何设置屏蔽文件

现在,你将拥有所有的资源ID的ID内的ArrayList

i have many text file and want to put it in for loop ! i get an Extra that have Resource name from last activity (s) and have an array that have Resource name of my text file from raw Resource is start from {d0,d1,d2,d3, ...,d79} and i want to check name(s) and array name and put the find name to resource!!! i have error on (res=R.raw.(d[i])) my code :

    int res = 0;
    for (int i = 0; i <=79; i = i + 1) {
        if (s.equals(d[i])){
            res=R.raw.(d[i])
        }
    } 
    inputstream = getResources().openRawResource(res);

解决方案

You can use getIdentifier (String name, String defType, String defPackage) for fetching resource id dynamically,

ArrayList<Integer> id = new ArrayList<Integer>();
for (int i = 0; i <= 79; i++) {
  id.add(getResources().getIdentifier("d"+i, "raw", getPackageName()));
}

Now you will have all the resources id's inside id ArrayList