インポート済みの事前入力された sqlite db にテーブルがあり、行 A1、B1、A2、B2、A3、B3 があります。それらのコンテンツをいくつかのボタンにランダムに設定しました。ここで、ユーザーがボタンのペア (たとえば、button2 とそのボタン 5 の後に) をクリックしたときに、これらのボタンの内容が一致するかどうかを比較します。つまり、A1 と B1 をペアにし、A2 と B2 などもペアにする必要があります。したがって、それらの列が一致するかどうかを比較したいと思います。どうやってするか?これまでの私のコードは次のとおりです。
public class Game extends Activity implements View.OnClickListener{
LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();
private String generateWhereClause(){
StringBuilder result = new StringBuilder();
for (Long l : mAnsweredQuestions){
result.append(" AND _ID <> " + l);
}
return result.toString();
}
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
}
};
Button a1,a2,a3,a4,b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spojnice);
a1 = (Button) findViewById(R.id.bA1);
a2 = (Button) findViewById(R.id.bA2);
a3 = (Button) findViewById(R.id.bA3);
a4 = (Button) findViewById(R.id.bA4);
b1 = (Button) findViewById(R.id.bB1);
b2 = (Button) findViewById(R.id.bB2);
b3 = (Button) findViewById(R.id.bB3);
b4 = (Button) findViewById(R.id.bB4);
nextQuestion();
}
private void nextQuestion() {
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestData(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
List<String> labelsA = new ArrayList<String>();
List<String> labelsB = new ArrayList<String>();
labelsA.add(c.getString(2));
labelsA.add(c.getString(4));
labelsA.add(c.getString(6));
labelsA.add(c.getString(8));
labelsB.add(c.getString(3));
labelsB.add(c.getString(5));
labelsB.add(c.getString(7));
labelsB.add(c.getString(9));
Collections.shuffle(labelsA);
Collections.shuffle(labelsB);
a1.setText(labelsA.get(0));
a1.setTag(labelsA.get(0));
a1.setOnClickListener(clickListener);
a2.setText(labelsA.get(1));
a2.setOnClickListener(clickListener);
a3.setText(labelsA.get(2));
a3.setOnClickListener(clickListener);
b1.setText(labelsB.get(0));
b1.setOnClickListener(clickListener);
b2.setText(labelsB.get(1));
b2.setOnClickListener(clickListener);
b3.setText(labelsB.get(2));
b3.setOnClickListener(clickListener);
}
finally{
mDbHelper.close();
}
}
ここに私のdbヘルパーがあります:
public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "/data/data/rs.androidaplikacije.spojnice/databases/";
private static String DB_NAME ="pitanja.sqlite";// Database name
private static SQLiteDatabase mDataBase;
private final Context mContext;
private static final String KEY_ID = "_ID";
private static final String KEY_PITANJE = "PITANJE";
private static final String TABLE_NAME = "tblPitanja";
public DataBaseHelper(Context mojContext)
{
super(mojContext, DB_NAME, null, 1);// 1? its Database Version
DB_PATH = mojContext.getApplicationInfo().dataDir + "/databases/";
this.mContext = mojContext;
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
this.getReadableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
/*Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
*/
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
//Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return mDataBase != null;
}
@Override
public void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
Log.w("DataBaseHelper", "Upgrading database!!!!!");
onCreate(arg0);
}
}
そして私のテストアダプター:
public class TestAdapter
{
protected static final String TAG = "DataAdapter";
private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;
public TestAdapter(Context context)
{
this.mContext = context;
mDbHelper = new DataBaseHelper(mContext);
}
public TestAdapter createDatabase() throws SQLException
{
try
{
mDbHelper.createDataBase();
}
catch (IOException mIOException)
{
Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}
public TestAdapter open() throws SQLException
{
try
{
mDbHelper.openDataBase();
mDbHelper.close();
mDb = mDbHelper.getReadableDatabase();
}
catch (SQLException mSQLException)
{
Log.e(TAG, "open >>"+ mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close()
{
mDbHelper.close();
}
public Cursor getTestData(String whereClause)
{;
try
{
String sql ="SELECT * FROM tblPitanja WHERE 1 = 1 " + whereClause + " ORDER BY RANDOM() LIMIT 1";
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
catch (SQLException mSQLException)
{
Log.e(TAG, "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}
}