Storing image resource id in Sqlite database and retrieving it in int array

江枫思渺然 提交于 2019-12-13 10:53:56

问题


I am developing an online shopping app, where I am storing all the product details in Sqlite database, along with resource ids of product images. I am retrieving those ids(eg: R.drawable.image1), in order to display the images, stored in drawable folder.

The ids are retrieved precisely, but an exception occurs, as "InvalidNumberFormatException: R.drawable.image1". I have stored all the ids, as VARCHAR, in sqlite. How can I store these ids in an int array, after they are retrieved as string values ?

I am displaying these images and other product details using recyclerview. Any help would be appreciated. Thanks in advance.


回答1:


Just store name of image in database and use below code to get its id

private void loadImage(String mImageName, ImageView mImageIcon){
int resID = mContext.getResources().getIdentifier(mImageName , "drawable", mContext.getPackageName());
            if(resID!=0) {//The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
                mImageIcon.setImageResource(resID);
            }
}


来源:https://stackoverflow.com/questions/42992989/storing-image-resource-id-in-sqlite-database-and-retrieving-it-in-int-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!