我正在开发一个测验游戏,但我有一个现有的数据库文件,其中有问题和选择..我怎么能在我的程序中连接它。 怎么可能,我执行查询以在TextView中显示我的问题和按钮中的选项。
答案 0 :(得分:2)
制作测验游戏的分步指南:
第1步:
首先从SQLite Manager
创建数据库。制作1列主要索引,第2列问题,& 选项的第3,第4,第5,第6列。还有一列用于正确答案的相同字符串(用于正确答案的比较)。应该像图中所示:
将此filename.sqlite
复制粘贴到资源文件夹
第2步:
为获取和设置数据库创建一个新的Getter Setters类。It should consists of all the fields that you are trying to fetch.
第3步:
创建一个扩展SQLiteOpenHelper
的DBAdapter类。使用此代码:
public class DBAdapter extends SQLiteOpenHelper
{
static String name = "kbcquiz.sqlite";
static String path = "";
static ArrayList<kbc> a;
static SQLiteDatabase sdb;
@Override
public void onCreate(SQLiteDatabase db)
{
// Your database is already Created, so no need to add anything here
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// Database Not upgraded here
}
private DBAdapter(Context v)
{
super(v, name, null, 1);
path = "/data/data/" + v.getApplicationContext().getPackageName() + "/databases";
}
public boolean checkDatabase()
{
SQLiteDatabase db = null;
try
{
db = SQLiteDatabase.openDatabase(path + "/" + name, null, SQLiteDatabase.OPEN_READONLY);
} catch (Exception e)
{
e.printStackTrace();
}
if (db == null)
{
return false;
}
else
{
db.close();
return true;
}
}
public static synchronized DBAdapter getDBAdapter(Context v)
{
return (new DBAdapter(v));
}
public void createDatabase(Context v)
{
this.getReadableDatabase();
try
{
InputStream myInput = v.getAssets().open(name);
// Path to the just created empty db
String outFileName = path +"/"+ name;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e)
{
System.out.println(e);
}
}
public void openDatabase()
{
try
{
sdb = SQLiteDatabase.openDatabase(path + "/" + name, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (Exception e)
{
System.out.println(e);
}
}
// Method for fetching Data and storing it into ArrayList
public ArrayList<kbc> getQuestions()
{
//question is the table name, you can also use "SELECT * FROM question" in case
Cursor c1 = sdb.rawQuery("SELECT * FROM question ORDER BY RANDOM()", null);
a = new ArrayList<kbc>();
while (c1.moveToNext())
{
kbc q1 = new kbc(); // kbc is the getter setter class
//the below code can vary upon ur getter-setter methods & variables
q1.setSno(c1.getInt(0));
q1.setQuestion(c1.getString(1));
q1.setA(c1.getString(2));
q1.setB(c1.getString(3));
q1.setC(c1.getString(4));
q1.setD(c1.getString(5));
q1.setAnswer(c1.getString(6));
a.add(q1);//--Always remember to add the fetched elements of type ArrayList<GetterGetter>
}
return a;
}
}
第4步:
在MainActivity中使用此POC
DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());
if (!db.checkDatabase())
{
db.createDatabase(getApplicationContext());
}
db.openDatabase();
q = db.getQuestions();
要从arraylist中获取任何字符串,请使用q.get(0).getQuestion(),q.get(0).getA(),q.get(0).getB(),q.get(0).getC(),q.get(0).getD()
更改索引以获得不同的colums&amp;将该文本设置为TextView或Button。
单击选择答案,您可以通过
检查正确性if(btn4.getText().toString().equals(q.get(e).getAnswer().toString()))
{
// correct answer
//Use it for changing color,managing indexing, etc..
}
输出:
希望这有帮助! 干杯