我正在尝试用SQLite数据库编写一个Android应用程序。我有4个活动。我想根据数据库中的特定情况启动这些活动。 这是我的数据库架构: 在目前的情况下;
…
listem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myintentGameA = new Intent(view.getContext(),GameA.class);
startActivity(myintentGameA);
}
});
...
我在我的数据库中有5个列表项。使用上面的代码,当我单击程序中的第一个列表项时,我可以启动我的GameA活动,我可以看到数字
... 2,17,24,25,3
我想创建我的其他意图;
Intent myintentGameB = new Intent(view.getContext(),GameB.class);
Intent myintentGameC = new Intent(view.getContext(),GameC.class);
Intent myintentGameD = new Intent(view.getContext(),GameD.class);
当我点击第二个列表项时,我想启动myinentGameC活动并查看数字
7,14,23,27,30,..
依此类推......我无法用光标函数编写正确的if else条件。我认为它应该是这样的:
任何帮助将不胜感激,谢谢你。
答案 0 :(得分:1)
一个选项是让SQL查询完成工作。例如,以下查询: -
SELECT
CASE
WHEN gamea IS NOT NULL THEN 'gamea'
WHEN gameb IS NOT NULL then 'gameb'
WHEN gamec IS NOt NULL then 'gamec'
WHEN gamed IS NOT NULL then 'gamed'
END AS game,
CASE
WHEN gamea IS NOT NULL THEN gamea
WHEN gameb IS NOT NULL THEN gameb
WHEN gamec IS NOT NULL THEN gamec
WHEN gamed IS NOT NULL then gamed
END AS gamevalue
FROM gamestart
WHERE id = 1
(其中WHERE id = 1中的 1 将是单击列表项时获取的行的id)
然后返回(通过光标): -
<强>&#34; gamea&#34; &#34; 2,17,24,25,3 ...&#34; (游戏?在名为游戏的列中,名为 gamestart <的列中的值/强>)
所以你提取要开始的游戏和要使用的值。
所有组合(即省略WHERE子句)是: -
要在Android中使用该查询,您可以使用以下方法: -
public Cursor getGameToStart(long id) {
String game_column = "CASE " +
"WHEN gamea IS NOT NULL THEN 'gamea' " +
"WHEN gameb IS NOT NULL then 'gameb' " +
"WHEN gamec IS NOT NULL then 'gamec' " +
"WHEN gamed IS NOT NULL then 'gamed' " +
"END AS " + TEMPCOL_GAME;
String gamevalues_column = "CASE " +
"WHEN gamea IS NOT NULL THEN gamea " +
"WHEN gameb IS NOT NULL THEN gameb " +
"WHEN gamec IS NOT NULL THEN gamec " +
"WHEN gamed IS NOT NULL then gamed " +
"END AS " + TEMPCOL_GAMEVALUES;
String[] columns = new String[]{game_column,gamevalues_column};
String whereclause = COL_ID + "=?";
String[] whereargs = new String[]{String.valueOf(id)};
return mDB.query(TBL_GAMESTART,columns,whereclause,whereargs,null,null,null);
}
以下用于测试上述内容(复制选择所有列表项1-5): -
SO49863106DBHelper dbhlpr = new SO49863106DBHelper(this);
for (long i=1; i < 6; i++) {
Cursor csr = dbhlpr.getGameToStart(i);
Log.d("GAMEINFO","Game Number = " + String.valueOf(i) + " Rows Extracted = " + String.valueOf(csr.getCount()));
while (csr.moveToNext()) {
Log.d("GAMEINFO",
"\n\t Game is " + csr.getString(csr.getColumnIndex(SO49863106DBHelper.TEMPCOL_GAME)) +
" Values are " + csr.getString(csr.getColumnIndex(SO49863106DBHelper.TEMPCOL_GAMEVALUES))
);
}
csr.close();
}
结果输出为: -
04-16 21:57:49.051 1377-1377/? D/GAMEINFO: Game Number = 1 Rows Extracted = 1
Game is gamea Values are 2,17,24,25,3...
Game Number = 2 Rows Extracted = 1
Game is gamec Values are 7,14,23,27,30,...
04-16 21:57:49.055 1377-1377/? D/GAMEINFO: Game Number = 3 Rows Extracted = 1
Game is gamed Values are 4,11,21,27,45,...
Game Number = 4 Rows Extracted = 1
Game is gamec Values are 2,5,23,47,51,...
Game Number = 5 Rows Extracted = 1
04-16 21:57:49.059 1377-1377/? D/GAMEINFO: Game is gamed Values are 7,10,17,21,33,...
上面使用的Databasehelper SO49863106DBHelper.java 是: -
public class SO49863106DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "games";
public static final int DBVERSION = 1;
public static final String TBL_GAMESTART = "gamestart";
public static final String COL_ID = BaseColumns._ID;
public static final String COL_GAMEA = "gamea";
public static final String COL_GAMEB = "gameb";
public static final String COL_GAMEC = "gamec";
public static final String COL_GAMED = "gamed";
public static final String TEMPCOL_GAME = "game";
public static final String TEMPCOL_GAMEVALUES = "gamevalues";
SQLiteDatabase mDB;
public SO49863106DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
//Table Create SQL
public void onCreate(SQLiteDatabase db) {
String crtsql = "CREATE TABLE IF NOT EXISTS " + TBL_GAMESTART +
"(" +
COL_ID + " INTEGER PRIMARY KEY," +
COL_GAMEA + " TEXT," +
COL_GAMEB + " TEXT," +
COL_GAMEC + " TEXT, " +
COL_GAMED + " TEXT" +
")";
//Insert the Data SQL
String insertsql = "INSERT INTO " + TBL_GAMESTART +
"(" + COL_GAMEA + "," + COL_GAMEB + "," + COL_GAMEC + "," + COL_GAMED + ") VALUES" +
"('2,17,24,25,3...',null,null,null)," +
"(null,null,'7,14,23,27,30,...',null)," +
"(null,null,null,'4,11,21,27,45,...')," +
"(null,null,'2,5,23,47,51,...',null)," +
"(null,null,null,'7,10,17,21,33,...')"
;
db.execSQL(crtsql); // Create the table
db.execSQL(insertsql); //add the data
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getGameToStart(long id) {
String game_column = "CASE " +
"WHEN gamea IS NOT NULL THEN 'gamea' " +
"WHEN gameb IS NOT NULL then 'gameb' " +
"WHEN gamec IS NOT NULL then 'gamec' " +
"WHEN gamed IS NOT NULL then 'gamed' " +
"END AS " + TEMPCOL_GAME;
String gamevalues_column = "CASE " +
"WHEN gamea IS NOT NULL THEN gamea " +
"WHEN gameb IS NOT NULL THEN gameb " +
"WHEN gamec IS NOT NULL THEN gamec " +
"WHEN gamed IS NOT NULL then gamed " +
"END AS " + TEMPCOL_GAMEVALUES;
String[] columns = new String[]{game_column,gamevalues_column};
String whereclause = COL_ID + "=?";
String[] whereargs = new String[]{String.valueOf(id)};
return mDB.query(TBL_GAMESTART,columns,whereclause,whereargs,null,null,null);
}
}