如何只查询部分数据库?

时间:2013-03-18 22:54:41

标签: java android sqlite

有没有办法只查询我的数据库部分?让我解释一下......我有一张桌子,如下图所示,我在那里有问题和答案。 A是问题,B是答案。所以,为了避免两个数据库,我创建了一个。我需要8个setText到8个按钮的答案,而在另一行我需要8个答案setText到其他8个按钮。所以,有了两个db,我会随机选择一行,集合将它们洗牌,并将setText设置为按钮,但是图片中有答案,我不知道该怎么做。因此,我需要随机选择一个id,选择问题,将它们随机播放,将setText设置为按钮,在同一行中选择所有8个答案,将它们随机播放,然后设置为其他8个按钮。有没有办法或者我需要再创建一个表和一个数据库助手类?

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,这就是我到目前为止所做的工作,但如果有更好的解决方案,请纠正我。我跳过了代码的一些相关部分。

Button a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8;
    TextView pitanje;

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{ 

            mDbHelper.open();

            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{
            mDbHelper.close();
        }