Home > Blockchain >  How can i get an image from drawable using image name stored in SQLite?
How can i get an image from drawable using image name stored in SQLite?

Time:01-13

I am trying to develop an Android quiz app using SQLite.My Database has a table containing the Questions and answers. 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. 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:

 `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

enter image description here

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";

`

enter image description here

The debugger enter image description here

Drawable

enter image description here

CodePudding user response:

Drawable id is an integer and it's not a string that you can convert to integer your code must be this:

Context context =tvImgQuest.getContext();
int id = context.getResources().getIdentifier(name, "drawable", context .getPackageName());
tvImgQuest.setImageResource(id);
  •  Tags:  
  • Related