我正在使用SQLite数据库构建一个测验游戏。目前问题以文字显示,我想用图像作为问题。点击"图片示例"下面看看我的意思。左边的图像是我的应用程序现在如何,但我想让它像右边的图像。我怎么能这样做?
这就是数据库的样子
package com.example.sqz;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class QuizHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;// constant for version of database
private static final String DATABASE_NAME = "SQZ.db";// constant for database name
private static final String TABLE_QUEST = "quest";// constant for table name
// constant for identifying table and columns
private static final String KEY_ID = "questionid";
private static final String COL_QUES = "question";
private static final String COL_ANSWER = "answer"; // correct answer
private static final String COL_ANSWER1 = "ANSWER1"; // answer 1
private static final String COL_ANSWER2 = "ANSWER2"; // answer 2
private static final String COL_ANSWER3 = "ANSWER3"; // answer 3
private static final String COL_ANSWER4 = "ANSWER4"; // answer 4
private static final String LEVEL = "LEVEL";
private SQLiteDatabase mydbase;
public QuizHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
mydbase = db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_QUES
+ " TEXT, " + COL_ANSWER + " TEXT, " + COL_ANSWER1 + " TEXT, "
+ COL_ANSWER2 + " TEXT, " + COL_ANSWER3 + " TEXT, " + COL_ANSWER4 + " TEXT, "+ LEVEL+ " TEXT)";
db.execSQL(sql);
addQuestion();
// db.close();
}
private void addQuestion() {
Question q1 = new Question("Which team won FIFA world cup in 2002 ?", "Brazil", "England","Germany", "Italy", "Brazil",1);
this.addQuestion(q1);
Question q2 = new Question("How many goals Messi scored UEFA Champions League 2015 ?", "7", "6", "5", "8", "6",1);
this.addQuestion(q2);
Question q3 = new Question("Which team won cricket world cup in 2015 ?", "Australia", "England","New Zealand", "South Africa", "Australia",1);
this.addQuestion(q3);
Question q4 = new Question("Which team won premier league in 2015 ?", "Arsenal", "Manchester city", "Liverpool", "Chelsea", "Chelsea",1);
this.addQuestion(q4);
Question q5 = new Question("What team does LeBron James play for ?", "Cleveland Cavaliers", "Charlotte Hornets", "Los Angeles Clippers", "Miami Heat", "Cleveland Cavaliers",1);
this.addQuestion(q5);
Question q6 = new Question("Who scored the most goals in 2013 ?", "Lionel Messi", "Zlatan Ibrahimovic", "thierry henry", "Cristiano Ronaldo", "Cristiano Ronaldo",1);
this.addQuestion(q6);
Question q7 = new Question("Which team won world twenty20 in 2012 ?", "Australia", "West Indies", "South Africa", "Sri Lanka", "West Indies",1);
this.addQuestion(q7);
Question q8 = new Question("Who won Formula 1 championship in 2013 ?", "Fernando Alonso", "Sebastian Vettel", "Esteban Gutierrez", "Lewis Hamilton", "Sebastian Vettel",1);
this.addQuestion(q8);
Question q9 = new Question("Who won world darts championship in 2015 ?", "Gary Anderson", "Phil Taylor", "Boris Koltsov", "Michael van Gerwen", "Gary Anderson",1);
this.addQuestion(q9);
Question q10 = new Question("Cristiano Ronaldo played for which team in 2007 ?", "Arsenal", "Chelsea", "Liverpool", "Man United", "Man United",1);
this.addQuestion(q10);
Question q11 = new Question("how many goals Steven gerrard has scored for liverpool ?", "186", "195", "188", "174", "186",1);
this.addQuestion(q11);
Question q12 = new Question("Steve Smith plays for which cricket team ?", "England", "New Zealand", "South Africa", "Australia", "Australia",1);
this.addQuestion(q12);
Question q13 = new Question("How many goals Neymar scored for Santos ?", "137", "136", "140", "138", "136",1);
this.addQuestion(q13);
Question q14 = new Question("Steven Finn plays for which cricket team ?", "England", "New Zealand", "South Africa", "Australia", "England",1);
this.addQuestion(q14);
Question q15 = new Question("Which football team Wayne Rooney play for ?", "Man City", "Liverpool", "Man United", "Arsenal", "Man United",1);
this.addQuestion(q15);
Question q16 = new Question("Which tennis player was ranked number 1 in 2016 ?", "Roger Federer", "Tomas Berdych", "Novak Djokovic", "Rafael Nadal", "Novak Djokovic",1);
this.addQuestion(q16);
Question q17 = new Question("Who won Wimbledon Championships ? 2013", "Roger Federer", "Tomas Berdych", "Novak Djokovic", "Andy Murray", "Andy Murray",1);
this.addQuestion(q17);
Question q18 = new Question("Which team won FIFA world cup in 2014 ?", "Spain", "Germany", "Brazil", "Italy", "Germany",1);
this.addQuestion(q18);
Question q19 = new Question("Which team won twenty20 cricket in 2009 ?", "Pakistan", "Indian", "England", "Sri Lanka", "Pakistan",1);
this.addQuestion(q19);
Question q20 = new Question("Which team won cricket world cup in 2007 ?", "India", "Sri Lanka", "New Zealnad", "Australia", "Australia",1);
this.addQuestion(q20);
Question q21 = new Question("Who won tour de france 2014 ?", "Bradley Wiggins", "Leopold Konig", "Vincenzo Nibali", "Alberto Contador", "Vincenzo Nibali",1);
this.addQuestion(q21);
// END
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);// Drop older table if existed
onCreate(db); // Create another table
}
// Adding new question
public void addQuestion(Question quest) {
// SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_QUES, quest.getQUESTION());
values.put(COL_ANSWER, quest.getANSWER());
values.put(COL_ANSWER1, quest.getA1());
values.put(COL_ANSWER2, quest.getA2());
values.put(COL_ANSWER3, quest.getA3());
values.put(COL_ANSWER4, quest.getA4());
values.put(LEVEL, quest.getLevel());
// Inserting Row
mydbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> questionList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
mydbase = this.getReadableDatabase();
Cursor cursor = mydbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setASWR1(cursor.getString(3));
quest.setASWR2(cursor.getString(4));
quest.setASWR3(cursor.getString(5));
quest.setASWR4(cursor.getString(6));
questionList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=0;i<20;i++)
list.add(i);
Collections.shuffle(list, new Random());
List<Question> shuffledQuestionList = new ArrayList<Question>();
for(int i=0;i<20;i++)
shuffledQuestionList.add(questionList.get(list.get(i)));
return shuffledQuestionList;
}
public List<Question> getAllQuestionsByLevel(int level) {
List<Question> questionList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST+" where "+LEVEL+" ='"+String.valueOf(level)+"'";
mydbase = this.getReadableDatabase();
Cursor cursor = mydbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setASWR1(cursor.getString(3));
quest.setASWR2(cursor.getString(4));
quest.setASWR3(cursor.getString(5));
quest.setASWR4(cursor.getString(6));
quest.setLevel(cursor.getInt(7));
if(quest.getLevel()==level)
questionList.add(quest);
} while (cursor.moveToNext());
}
return questionList;
// return quest list
//ArrayList<Integer> list=new ArrayList<Integer>();
//for(int i=0;i<5;i++)
// list.add(i);
/// Collections.shuffle(list);
// List<Question> shuffledQuestionList = new ArrayList<Question>();
// for(int i=0;i<5;i++)
// shuffledQuestionList.add(questionList.get(list.get(i)));
//return shuffledQuestionList;
}
}
答案 0 :(得分:1)
我建议将图像在线存储在服务器上,然后在数据库中有一个指向正确图像的URL。如果您希望将图像存储在本地设备上,则可以在启动应用程序时缓存它们。只需查找数据库链接并在本地下载每个链接。让它们在线托管还可以让您灵活地在本地缓存它们,或者只是简单地在旅途中显示它们。
编辑:您当然可以将所有图像添加到资源文件夹,然后只需在数据库中添加指针即可指向正确的图像。此解决方案完全脱机。
答案 1 :(得分:0)
如果您想让所有内容脱机,您有两种选择:
将图像名称(或提示)存储到数据库,然后根据名称将资源中的图像加载到新的图像视图中并将其嵌套到问题容器中。
将图像转换为base64(基本上是文本表示),并将其作为文本存储到数据库中。一旦你将它转换回位图并将位图drawable设置为新的图像视图,然后将其嵌入到问题容器中。