英文:
Get image path from SQLITE recycler view Error
问题
以下是翻译好的部分:
有谁能帮助我理解我的错误吗?
我试图实现的目标是从SQLite数据库获取图像路径并插入到RecyclerView中。包含所有图像的文件夹将位于res/drawable中。
在适配器类中,我已经声明了`Context并将其设置为私有全局变量`,并在onCreateViewHolder中添加了`context = parent.getContext();`。
还在DBModel类中,我已经将所有地方从位图更改为字符串,并且在DB文件中从blob更改为TEXT。
public void onBindViewHolder(@NonNull DBViewHolder holder, int position) {
ModelClass objModelClass = objModelClassArrayList.get(position);
holder.description_sign.setText(objModelClass.getDescription());
//context = holder.image_sign.getContext();
//int resAd = getIdentifier(image_path,"drawable",context.getPackageName());
int i = context.getResources().getIdentifier(objModelClass.getImage(),"drawable",context.getPackageName());
holder.image_sign.setImageResource(i);
//holder.image_sign.setImageBitmap(objModelClass.getImage()); <- 它有效,但我不想要那个。
}
> E/AndroidRuntime: 致命异常: 主线程
> 进程: com.example.dissertation, PID: 5449
> java.lang.NullPointerException: 名称为空
> 在 android.content.res.ResourcesImpl.getIdentifier(ResourcesImpl.java:240)
> 在 android.content.res.Resources.getIdentifier(Resources.java:1927)
英文:
Can anyone help me to understand my mistake?
What I am trying to achieve is to get the image path from SQLite dB and insert into recycler view. Folder with all images will be in res/drawable.
In adapter class, I have declared Context and is private global
and in onCreateViewHolder, I added context = parent.getContext();
Also in DBModel class, I changed everywhere from bitmap to string and in DB file from blob to TEXT.
public void onBindViewHolder(@NonNull DBViewHolder holder, int position) {
ModelClass objModelClass = objModelClassArrayList.get(position);
holder.description_sign.setText(objModelClass.getDescription());
//context = holder.image_sign.getContext();
//int resAd = getIdentifier(image_path,"drawable",context.getPackageName());
int i = context.getResources().getIdentifier(objModelClass.getImage(),"drawable",context.getPackageName());
holder.image_sign.setImageResource(i);
//holder.image_sign.setImageBitmap(objModelClass.getImage()); <- It works but i don't want that.
}
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.example.dissertation, PID: 5449
> java.lang.NullPointerException: name is null
> at android.content.res.ResourcesImpl.getIdentifier(ResourcesImpl.java:240)
> at android.content.res.Resources.getIdentifier(Resources.java:1927)
答案1
得分: 0
String uri = "@drawable/" + objModelClass.getImage();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable res = context.getResources().getDrawable(imageResource);
holder.image_sign.setImageDrawable(res);
英文:
Could you try this code block?
String uri = "@drawable/" + objModelClass.getImage();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable res = context.getResources().getDrawable(imageResource);
holder.image_sign.setImageDrawable(res);
答案2
得分: 0
ModelClass objModelClass = objModelClassArrayList.get(position);
holder.description_sign.setText(objModelClass.getDescription());
String uri = "@drawable/" + objModelClass.getImage();
int imageResource = holder.itemView.getResources().getIdentifier(uri, null, holder.itemView.getContext().getPackageName());
holder.image_sign.setImageResource(imageResource);
还需要在onCreateViewHolder中将Context声明为全局变量,并在其中添加以下代码以引用它:context = parent.getContext();
英文:
ModelClass objModelClass = objModelClassArrayList.get(position);
holder.description_sign.setText(objModelClass.getDescription())
String uri = "@drawable/" + objModelClass.getImage();
int imageResource = holder.itemView.getResources().getIdentifier(uri, null, holder.itemView.getContext().getPackageName());
holder.image_sign.setImageResource(imageResource);
It should also be declared Context as global and in onCreateViewHolder to refer to it context = parent.getContext();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论