从SQLITE回收站视图获取图像路径错误。

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

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,&quot;drawable&quot;,context.getPackageName());
    int i = context.getResources().getIdentifier(objModelClass.getImage(),&quot;drawable&quot;,context.getPackageName());
    holder.image_sign.setImageResource(i);
    
  
    
        //holder.image_sign.setImageBitmap(objModelClass.getImage());  &lt;- It works but i don&#39;t want that.
    }


&gt;  E/AndroidRuntime: FATAL EXCEPTION: main
&gt;             Process: com.example.dissertation, PID: 5449
&gt;             java.lang.NullPointerException: name is null
&gt;                 at android.content.res.ResourcesImpl.getIdentifier(ResourcesImpl.java:240)
&gt;                 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 = &quot;@drawable/&quot; + 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 = &quot;@drawable/&quot; + 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();

huangapple
  • 本文由 发表于 2020年5月31日 00:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/62105402.html
匿名

发表评论

匿名网友

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

确定