问题
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