1

I'm currently trying to make an Simple Application Quiz, that will have an image attached to each question with 4 answers, so i have about 100++ images to store in the database

So far i can figure out how to make the questions without an image on each question trough this tutorial here : Chuck Application Part 1

i've found out a way to store an image path is by hardcoding the path to the textfields in the SQLite Database from this links below

Saving resources path in SQLite

According to the link above, he stated that he saves the reference of the image by typing "R.drawable.picture1" to a text field inside the SQLite Database,

if it's true, is it the correct way to do so (by saving the image in drawable folders and writing the path to the text field)? if it's not then what's the correct way to do so,

how do i construct the image path (setting the setter and getter methods) for getting the field from the database?,

and how do i set the path to an imageview?

any help will be appreciated, thankyou in advance

UPDATE

so i learned from another link how to use a string for reference and i edited the string below, where ImageI.getImage1(); refers to a row from my database that contains my image name

private void showImage() {
        String imagea = ImageI.getImage1();
        int imageResource = getResources().getIdentifier(imagea , "drawable", context.getPackageName());
        ImageView gambar = (ImageView) findViewById(R.id.imagetest);
         Drawable image = getResources().getDrawable(imageResource);
         gambar.setImageDrawable(image);
    }

but i always got a force close, where did i do wrong?
so far i've tried writing in my database row

R.Drawable.picture
Drawable/picture
picture

but nothing works....

this is my query

    public List<ImageQuestions> getImageQuestionSet(int ingroup, int numQ) {
        List<ImageQuestions> questionSet = new ArrayList<ImageQuestions>();
        Cursor c = myDataBase.rawQuery("SELECT * FROM IMAGETEST WHERE INGROUP=" + ingroup +
                " ORDER BY RANDOM() LIMIT " + numQ, null);
         while (c.moveToNext()){      
                //Log.d("IMAGEQUESTIONS", "Images Found in DB: " + c.getString(1));
               ImageQuestions I = new ImageQuestions();
               I.setQuestion(c.getString(1));
               I.setImage1(c.getString(2));
               I.setAnswer(c.getString(3));
               I.setOption1(c.getString(4));
               I.setOption2(c.getString(5));
               I.setGroup(ingroup);
               questionSet.add(I);
           }         
           return questionSet;
        }
}

this is my log cat

07-18 03:28:41.283: W/dalvikvm(647): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-18 03:28:41.293: E/AndroidRuntime(647): FATAL EXCEPTION: main
07-18 03:28:41.293: E/AndroidRuntime(647): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
07-18 03:28:41.293: E/AndroidRuntime(647):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
07-18 03:28:41.293: E/AndroidRuntime(647):  at java.util.ArrayList.get(ArrayList.java:311)
07-18 03:28:41.293: E/AndroidRuntime(647):  at learning.everything.quiz.GamePlayI.getNextQuestion(GamePlayI.java:113)
07-18 03:28:41.293: E/AndroidRuntime(647):  at learning.everything.BigAss.onCreate(BigAss.java:32)
07-18 03:28:41.293: E/AndroidRuntime(647):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 03:28:41.293: E/AndroidRuntime(647):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)  

i realized its caused by getting an arraylist from the database so here is my GamePlayI class

    public class GamePlayI {

    private List<ImageQuestions> questions = new ArrayList<ImageQuestions>();   

    public void setQuestions(List<ImageQuestions> questions) {
        this.questions = questions;
        }


    public void addQuestions(ImageQuestions I) {
        this.questions.add(I);
    }


    public List<ImageQuestions> getQuestions() {
        return questions;
    }


    public ImageQuestions getNextQuestion() {

        //get the question 
        //below is line 113\\
        ImageQuestions next = questions.get(this.getRound());
        //update the round number to the next round
        this.setRound(this.getRound()+1);
        return next;

    }

}

    public void setNumRounds(int numRounds) {
    this.numRounds = numRounds;
        }

    public int getNumRounds() {
    return numRounds;
}
Community
  • 1
  • 1
Monty
  • 13
  • 1
  • 5

1 Answers1

-1

I would only save the image name in the db and then get the resource identifier with context.getResources().getIdentifier(). Check out this similar question.

Community
  • 1
  • 1
SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • Thank you for your answer. Can you be more specific please. How can i do that? and is there a way to put the context.getResources().getIdentifier() in a cursor? @SimonSays – Monty Jul 12 '13 at 06:17
  • Maybe i do not completely understand your question. You want 1. get the image name from the DB, 2. get the image resource id from the name and 3. set the image resource to an ImageView. Isn't that pretty much what the SO link in your post does? So what exactly is the problem? And what do you mean with "put the context.getResources().getIdentifier() in a cursor"? You can not put anything in the cursor? you want to save the identifier to the DB? Don't do that! identifiers can change every time you build the app. – SimonSays Jul 12 '13 at 16:42
  • i'm really new in android so please do bear with my questions and correct me if i'm wrong, yes number 1,2,& 3 is correct, my problem is i dont know the right way to do so, please disregard my comment above because by the time i post this comment is the time i know what get.Resources().getIdentifier is for, i learned how to do number 1,2 & 3 so please check my edited question @SimonSays – Monty Jul 13 '13 at 15:12
  • What do you get as `String imagea`? What as `int imageResource`? What is the exception? Post your log cat. – SimonSays Jul 15 '13 at 17:59
  • 'String imagea' contains a string which is my image name, well the 'int imageResource' i got is from here [link](http://stackoverflow.com/questions/2349652/android-open-resource-from-drawable-string), and i've posted my exception, please check the log cat @SimonSays – Monty Jul 17 '13 at 20:54
  • Looks like you have more rounds than you have questions. You try to get a question with an index that does not exist in the List. `ImageQuestions next = questions.get(this.getRound());` – SimonSays Jul 17 '13 at 23:51