Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何比较从SQLite数据库填充的按钮?_Java_Android_Sqlite - Fatal编程技术网

Java 如何比较从SQLite数据库填充的按钮?

Java 如何比较从SQLite数据库填充的按钮?,java,android,sqlite,Java,Android,Sqlite,好的,我已经成功地将预填充的sqlite数据库导入到我的Assets文件夹并从中读取,还将我的问题随机设置为我的TextView,左侧按钮设置一列,右侧按钮设置B列。下面您可以看到我的dbhelper、testhelper和我的游戏类。我被困在这里了。我需要比较左侧和右侧,当用户点击一侧的按钮时,例如A4上的按钮,为了正确,他需要点击B4。我如何比较这个 数据库帮助程序: public class DataBaseHelper extends SQLiteOpenHelper { private

好的,我已经成功地将预填充的sqlite数据库导入到我的Assets文件夹并从中读取,还将我的问题随机设置为我的TextView,左侧按钮设置一列,右侧按钮设置B列。下面您可以看到我的dbhelper、testhelper和我的游戏类。我被困在这里了。我需要比较左侧和右侧,当用户点击一侧的按钮时,例如A4上的按钮,为了正确,他需要点击B4。我如何比较这个

数据库帮助程序:

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;
         }
     }
}
我的游戏课:

public class Spojnice 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,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8;
    TextView pitanje;

    @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();
    }

    private void nextQuestion() {

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

        try{    //Pokusava da otvori db

            mDbHelper.open();  //baza otvorena

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

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

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

            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));
            labelsA.add(c.getString(10));
            labelsA.add(c.getString(12));
            labelsA.add(c.getString(14));
            labelsA.add(c.getString(16));

            labelsB.add(c.getString(3));
            labelsB.add(c.getString(5));
            labelsB.add(c.getString(7));
            labelsB.add(c.getString(9));
            labelsB.add(c.getString(11));
            labelsB.add(c.getString(13));
            labelsB.add(c.getString(15));
            labelsB.add(c.getString(17));

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

            a1.setText(labelsA.get(0));
            a1.setOnClickListener(clickListener);

            a2.setText(labelsA.get(1));
            a2.setOnClickListener(clickListener);

            a3.setText(labelsA.get(2));
            a3.setOnClickListener(clickListener);

            a4.setText(labelsA.get(3));
            a4.setOnClickListener(clickListener);

            a5.setText(labelsA.get(4));
            a5.setOnClickListener(clickListener);

            a6.setText(labelsA.get(5));
            a6.setOnClickListener(clickListener);

            a7.setText(labelsA.get(6));
            a7.setOnClickListener(clickListener);

            a8.setText(labelsA.get(7));
            a8.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);

            b4.setText(labelsB.get(3));
            b4.setOnClickListener(clickListener);

            b5.setText(labelsB.get(4));
            b5.setOnClickListener(clickListener);

            b6.setText(labelsB.get(5));
            b6.setOnClickListener(clickListener);

            b7.setText(labelsB.get(6));
            b7.setOnClickListener(clickListener);

            b8.setText(labelsB.get(7));
            b8.setOnClickListener(clickListener);

    }
        finally{    // kada zavrsi sa koriscenjem baze podataka, zatvara db
            mDbHelper.close();
        }


}

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
}

如果要从A4执行B4的单击,则可以在A4.1的onClick中使用B4.performClick。什么是比较按钮?您是否在确定下一步应该单击哪个按钮时遇到了问题,例如B1?并考虑使用数组按钮[] a,按钮[]b按钮代替A1,A2…这不仅是因为这是一种良好的代码实践,而且您还可以使用索引来引用按钮:例如,如果单击了按钮a[i],那么接下来应该单击按钮b[i]。您能否在下面给出一个代码示例,以回答您的问题,我不明白您说了什么?通过比较按钮选项…假设你必须连接演员…如果你点击左边的西尔维斯特,你需要点击右边的史泰龙才能正确。例如,在我的数据库中,西尔维斯特是A3,史泰龙是B3。