[英]How can i get an image from drawable using image name stored in SQLite?
I am trying to develop an Android quiz app using SQLite.My Database has a table containing the Questions and answers.我正在尝试使用 SQLite 开发一个 Android 测验应用程序。我的数据库有一个包含问题和答案的表。 Questions may have text or images.问题可能有文字或图像。 So i put images in drawable and the name in a column of Quiz table.因此,我将图像放入可绘制对象中,并将名称放入测验表的列中。 I tried to load the content of the image (using the value of that column) in the ImageView associated to each one of my quiz questions but im not sure if i used the right code.I don't want to use blobs.我尝试在与我的每个测验问题相关联的 ImageView 中加载图像的内容(使用该列的值),但我不确定我是否使用了正确的代码。我不想使用 blob。 Any suggestion?有什么建议吗? Any answer will be much appreciated.任何答案将不胜感激。 Thank you.谢谢你。
That's what i thought to write so to call the image from db and drawable:这就是我想写的,以便从 db 和 drawable 调用图像:
`String y = "R.drawable."+currentQuestion.getImage();
tvImgQuest.setImageResource(Integer.parseInt (y));`
and this is the method i use:这是我使用的方法:
`private void showNextQuestion()
{
radioButton1.setTextColor (textColorDefaultRb);
radioButton2.setTextColor (textColorDefaultRb);
radioButton3.setTextColor (textColorDefaultRb);
radioButton4.setTextColor (textColorDefaultRb);
radioGroup.clearCheck();
if (questionCounter < questionCountTotal )
{
currentQuestion = questionList.get(questionCounter);
tvQuestion.setText(currentQuestion.getQuestion ());
if(currentQuestion.getImage() == null || currentQuestion.getImage().isEmpty()) {
tvImgQuest.setVisibility (View.INVISIBLE);}
else{
ImageView tvImgQuest=(ImageView) findViewById(R.id.tvImgQuest);
tvImgQuest.setVisibility (View.VISIBLE);
String y = "R.drawable."+currentQuestion.getImage();
tvImgQuest.setImageResource(Integer.parseInt (y));
}
radioButton1.setText(currentQuestion.getAnswer1 ());
radioButton2.setText(currentQuestion.getAnswer2 ());
radioButton3.setText(currentQuestion.getAnswer3 ());
/**Hide radio button in case possible answers are three */
if(currentQuestion.getAnswer4() == null || currentQuestion.getAnswer4().isEmpty()) {
radioButton4.setVisibility(View.INVISIBLE);
} else {
radioButton4.setVisibility(View.VISIBLE);
radioButton4.setText(currentQuestion.getAnswer4());
}
questionCounter++;
tvQuestionCount.setText("Question: " + questionCounter + "/" + questionCountTotal);
answered = false;
butconfirmAns.setText("Submit");
}
else {
finishQuiz();
}
}`
The console控制台
The Table桌子
`public static class QuestionTable implements BaseColumns {
public static final String TABLE_NAME = "quiz_question";
public static final String COLUMN_QUESTION = "question";
public static final String COLUMN_IMAGE = "image";
public static final String COLUMN_ANSWER1 = "answer1";
public static final String COLUMN_ANSWER2 = "answer2";
public static final String COLUMN_ANSWER3 = "answer3";
public static final String COLUMN_ANSWER4 = "answer4";
public static final String COLUMN_ANSWERNUM = "answerNum";
` `
Drawable可绘制
Drawable id is an integer and it's not a string that you can convert to integer your code must be this:可绘制的 id 是 integer 并且它不是可以转换为 integer 的字符串,您的代码必须是这样的:
Context context =tvImgQuest.getContext();
int id = context.getResources().getIdentifier(name, "drawable", context .getPackageName());
tvImgQuest.setImageResource(id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.