0

我在导入的预填充 sqlite 数据库中有一个表,以及 A1、B1、A2、B2、A3、B3 行。我随机将它们的内容设置为一些按钮。现在我希望当用户单击一对按钮时,例如按钮 2 和之后的按钮 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();
        }


}

这是我的数据库助手:

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;
         }
     }
}
4

3 回答 3

1

在它们已经在按钮上之后,您不需要再次从数据库中检索值。

一个好的方法是在您开始时为每个按钮设置标签。为匹配的按钮设置相同的标签,并在单击时验证标签是否匹配。

按照您的结构,您可以从数据库中检索标签的值,对于labelsAand labelsB,它们可能是一个 Collection

class MyStruct {
    private String label;
    private String tag;
}

因此,初始化然后像

ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>();
ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>();

要添加每个值,请执行

// labelForButton1 and tagForButton1And
labelsA.add(new MyStruct("labelForButton1", "tagForButton1And2"); // this tag should be the same to button that matches
labelsB.add(new MyStruct("labelForButton2", "tagForButton1And2"); 

Collections.shuffle(labelsA);
Collections.shuffle(labelsB);

a1.setText(labelsA.get(0).getLabel());
a1.setTag(labelsA.get(0).getTag());

// and so on to a2, b2 etc

在侦听器中,您可以验证每个按钮的标记是否匹配。

于 2013-03-23T00:27:03.457 回答
0

所以让我们说用户首先选择了西尔维斯特......然后是史泰龙,你想确定它是否在数据库中吗?

http://sqlfiddle.com/#!7/f28af/1

因此,您可以做的是,当用户单击第一个按钮时,获取其值并将其存储在变量 var1 = 'Sylvester' 中。在第二次单击时,将该值存储在 var2 = 'Stallone' 中。现在您所要做的就是执行以下行并查看计数是否 > 0。

select count(*) from my_table where col1 = "Sylvester" and col2 = "stallone"
于 2013-03-22T02:32:49.687 回答
0

好的,这是我到目前为止所做的。

 class MyStruct {
       public MyStruct (String lab, String t){
        label = lab;
        tag = t;
    }
       private String label;
       private String tag;
       }



@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);   //full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        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);
        a5 = (Button) findViewById(R.id.bA5);
        a6 = (Button) findViewById(R.id.bA6);
        a7 = (Button) findViewById(R.id.bA7);
        a8 = (Button) findViewById(R.id.bA8);
        b1 = (Button) findViewById(R.id.bB1);
        b2 = (Button) findViewById(R.id.bB2);
        b3 = (Button) findViewById(R.id.bB3);
        b4 = (Button) findViewById(R.id.bB4);
        b5 = (Button) findViewById(R.id.bB5);
        b6 = (Button) findViewById(R.id.bB6);
        b7 = (Button) findViewById(R.id.bB7);
        b8 = (Button) findViewById(R.id.bB8);
        pitanje = (TextView) findViewById(R.id.tvPitanje);

        nextQuestion();

        }

    public void nextQuestion() {

        TestAdapter mDbHelper = new TestAdapter(this);
        mDbHelper.createDatabase();

        try{

            mDbHelper.open();

            Cursor c = mDbHelper.getTestData(generateWhereClause());

            mAnsweredQuestions.add(c.getLong(0));

            ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>();
            ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>();

            labelsA.add(new MyStruct(c.getString(2), "1")); // this tag should be the same to button that matches
            labelsB.add(new MyStruct(c.getString(3), "1"));
            labelsA.add(new MyStruct(c.getString(4), "2"));
            labelsB.add(new MyStruct(c.getString(5), "2"));
            labelsA.add(new MyStruct(c.getString(6), "3"));
            labelsB.add(new MyStruct(c.getString(7), "3"));
            labelsA.add(new MyStruct(c.getString(8), "4"));
            labelsB.add(new MyStruct(c.getString(9), "4"));
            labelsA.add(new MyStruct(c.getString(10), "5"));
            labelsB.add(new MyStruct(c.getString(11), "5"));
            labelsA.add(new MyStruct(c.getString(12), "6"));
            labelsB.add(new MyStruct(c.getString(13), "6"));
            labelsA.add(new MyStruct(c.getString(14), "7"));
            labelsB.add(new MyStruct(c.getString(15), "7"));
            labelsA.add(new MyStruct(c.getString(16), "8"));
            labelsB.add(new MyStruct(c.getString(17), "8"));

            Collections.shuffle(labelsA);
            Collections.shuffle(labelsB);

            pitanje.setText(c.getString(1));

            a1.setText(labelsA.get(0).label);
            a1.setTag(labelsA.get(0).tag);
            a1.setOnClickListener(clickListener);
            b1.setText(labelsB.get(0).label);
            b1.setTag(labelsB.get(0).tag);
            b1.setOnClickListener(clickListener);
            a2.setText(labelsA.get(1).label);
            a2.setTag(labelsA.get(1).tag);
            a2.setOnClickListener(clickListener);
            b2.setText(labelsB.get(1).label);
            b2.setTag(labelsB.get(1).tag);
            b2.setOnClickListener(clickListener);
            a3.setText(labelsA.get(2).label);
            a3.setTag(labelsA.get(2).tag);
            a3.setOnClickListener(clickListener);
            b3.setText(labelsB.get(2).label);
            b3.setTag(labelsB.get(2).tag);
            b3.setOnClickListener(clickListener);
            a4.setText(labelsA.get(3).label);
            a4.setTag(labelsA.get(3).tag);
            a4.setOnClickListener(clickListener);
            b4.setText(labelsB.get(3).label);
            b4.setTag(labelsB.get(3).tag);
            b4.setOnClickListener(clickListener);
            a5.setText(labelsA.get(4).label);
            a5.setTag(labelsA.get(4).tag);
            a5.setOnClickListener(clickListener);
            b5.setText(labelsB.get(4).label);
            b5.setTag(labelsB.get(4).tag);
            b5.setOnClickListener(clickListener);
            a6.setText(labelsA.get(5).label);
            a6.setTag(labelsA.get(5).tag);
            a6.setOnClickListener(clickListener);
            b6.setText(labelsB.get(5).label);
            b6.setTag(labelsB.get(5).tag);
            b6.setOnClickListener(clickListener);
            a7.setText(labelsA.get(6).label);
            a7.setTag(labelsA.get(6).tag);
            a7.setOnClickListener(clickListener);
            b7.setText(labelsB.get(6).label);
            b7.setTag(labelsB.get(6).tag);
            b7.setOnClickListener(clickListener);
            a8.setText(labelsA.get(7).label);
            a8.setTag(labelsA.get(7).tag);
            a8.setOnClickListener(clickListener);
            b8.setText(labelsB.get(7).label);
            b8.setTag(labelsB.get(7).tag);
            b8.setOnClickListener(clickListener);

        }


        finally{  
            mDbHelper.close();
        }
    }

在我班的顶部,我制作了 onClickListener,但我不知道如何继续。当我按下左侧按钮之一时,我不知道如何做到这一点,直到我按下右侧按钮。如果我按左键,然后再按左键怎么办?在这种情况下,我想忽略之前按下的左键并从最后按下的左键继续。

final OnClickListener clickListener = new OnClickListener() {
        public void onClick(View v) {

            MyStruct ms = (MyStruct) v.getTag();


            }
     };
于 2013-03-23T13:57:25.310 回答