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