Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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_Android_Json_Android Resources - Fatal编程技术网

从适配器中的字符串获取Android资源ID

从适配器中的字符串获取Android资源ID,android,json,android-resources,Android,Json,Android Resources,我试图从JSON数组中获取Android资源ID并将ImageView src设置为该特定ID 我正在使用以下代码 String str = mDataset.get(position).icon_res.substring(0, mDataset.get(position).icon_res.lastIndexOf('.')); int res = Resources.getSystem().getIdentifier("ic_" + str, "drawable", this

我试图从JSON数组中获取Android资源ID并将ImageView src设置为该特定ID

我正在使用以下代码

String str = mDataset.get(position).icon_res.substring(0, mDataset.get(position).icon_res.lastIndexOf('.'));
        int res = Resources.getSystem().getIdentifier("ic_" + str, "drawable", this.getClass().getPackage().getName());
        if(res == 0)
        {
            Log.d("is null", "null");
            Log.d("string = ", "ic_" + str);
            Log.d("package", this.getClass().getPackage().getName());
        }
但出于某种原因,它检测到resint为0。包名称似乎正确,字符串正确,但由于某些原因,它找不到正确的可绘制资源。我做错了什么

编辑:

完整适配器代码
包com.xyz
导入android.app.Activity;
导入android.app.Application;
导入android.content.Context;
导入android.content.res.Resources;
导入android.graphics.drawable.drawable;
导入android.media.Image;
导入android.support.constraint.ConstraintLayout;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.util.List;
/**
*由Kreso于2017年3月23日创建。。
*/
公共类CategoryAdapter扩展了RecyclerView.Adapter{
私有列表数据集;
私人语境;
公共类别适配器(列表分类列表){
mDataset=分类列表;
}
//提供对每个数据项的视图的引用
//复杂数据项可能需要每个项有多个视图,并且
//您可以访问视图持有者中数据项的所有视图
公共类ViewHolder扩展了RecyclerView.ViewHolder{
//在本例中,每个数据项只是一个字符串
公共文本视图txtHeader;
公共约束和保持架布局;
公共图像视图图标;
公共视图持有者(视图v){
超级(五);
txtHeader=(TextView)v.findViewById(R.id.firstLine);
holderLayout=(ConstraintLayout)v.findViewById(R.id.itemHolder);
icon=(ImageView)v.findViewById(R.id.icon);
context=v.getContext();
}
}
/*公共空添加(整型位置,字符串项){
mDataset.add(位置、项目);
(位置);
}*/
公共无效删除(内部位置){
mDataset.remove(位置);
已移除(位置)的项目;
notifyItemRangeChanged(位置,mDataset.size());
}
//提供合适的构造函数(取决于数据集的类型)
/*公共类别适配器(ArrayList myDataset){
mDataset=myDataset;
}*/
//创建新视图(由布局管理器调用)
@凌驾
public CategoryAdapter.ViewHolder onCreateViewHolder(视图组父级、,
int视图类型){
//创建新视图
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.recycler\u item\u行,parent,false);
//设置视图的大小、边距、填充和布局参数
视窗支架vh=新视窗支架(v);
返回vh;
}
//替换视图的内容(由布局管理器调用)
@凌驾
公共无效onBindViewHolder(ViewHolder,最终int位置){
//-在此位置从数据集中获取元素
//-用该元素替换视图的内容
最终字符串名称=mDataset.get(position).name;
holder.txtHeader.setText(mDataset.get(position.name));
//holder.icon.setImageResource(R.drawable.ic_-beer);
String str=mDataset.get(位置).icon_res.substring(0,mDataset.get(位置).icon_res.lastIndexOf('.');
int res=context.getResources();
如果(res==0)
{
Log.d(“为空”、“为空”);
Log.d(“string=”,“ic_u”+str);
Log.d(“package”,this.getClass().getPackage().getName());
}
holder.icon.setImageResource(res);
holder.holderLayout.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
移除(位置);
}
});
}
//返回数据集的大小(由布局管理器调用)
@凌驾
public int getItemCount(){
返回mDataset.size();
}
}

我通过使用稍微不同的方法解决了这个问题

// Remove file extension
        String str = mDataset.get(position).icon_res.substring(0, mDataset.get(position).icon_res.lastIndexOf('.'));
        // Replace - with underscore _
        str = str.replace("-", "_");

        int res = 0;
        // Try to find the resource with that name (icons in drawable folder)
        try {
            res = R.drawable.class.getField("ic_" + str).getInt(null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // if no icon is found
            holder.icon.setImageResource(R.drawable.ic_coffee);
        }
        // Set icon resource
        holder.icon.setImageResource(res);

使用以下代码从资源中获取图像:

int res = context.getResources().getIdentifier("ic_" + str, "drawable", context.getPackageName());

共享适配器
资源的完整代码。getSystem()
不是包的
资源。这就是它找不到你的标识符的原因。@MikeM。那我应该用什么?这有点让人困惑,我是第一次这么做。我的回答给出了要求的结果,但如果你的回答更好,我想知道更多关于背景的信息。Narendra Sorathiya的回答说明了如何获得你的
资源。也就是说,您需要在应用程序的
上下文中调用
getResources()
。不过,你的方法也行,所以你想用哪一种都行。“我只是在解释为什么您的原始代码失败了。”MikeM。我接受了纳伦德拉的回答,它更干净,而且从一开始就是一个愚蠢的错误。谢谢你们俩的帮助!
int res = context.getResources().getIdentifier("ic_" + str, "drawable", context.getPackageName());