我目前正在尝试制作一个简单的应用程序测验,每个问题都会附有一个带有 4 个答案的图像,所以我有大约 100++ 个图像要存储在数据库中
到目前为止,我可以通过本教程了解如何在每个问题上没有图像的情况下提出问题:Chuck Application Part 1
我发现了一种存储图像路径的方法是通过以下链接硬编码 SQLite 数据库中文本字段的路径
根据上面的链接,他说他通过在 SQLite 数据库内的文本字段中键入“R.drawable.picture1”来保存图像的引用,
如果是真的,这样做是否正确(通过将图像保存在可绘制文件夹中并将路径写入文本字段)?如果不是那么正确的方法是什么,
我如何构造图像路径(设置 setter 和 getter 方法)以从数据库中获取字段?
以及如何设置图像视图的路径?
任何帮助将不胜感激,在此先感谢您
更新
所以我从另一个链接中学习了如何使用字符串作为参考,我编辑了下面的字符串,其中 ImageI.getImage1(); 引用我的数据库中包含我的图像名称的行
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);
}
但我总是有一种力量接近,我哪里做错了?
到目前为止,我已经尝试在我的数据库行中写入
R.Drawable.picture
Drawable/picture
picture
但没有任何效果....
这是我的查询
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;
}
}
这是我的原木猫
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)
我意识到它是由从数据库中获取一个数组列表引起的,所以这是我的 GamePlayI 类
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;
}