无法插入从getextra收到的值

时间:2014-02-04 18:56:39

标签: android sqlite insert

我正在使用getextra和putextra检索上一个活动的值 但是在数据库中插入时,变量会丢失保存在其中的值或变为null。 我无法理解可能是什么问题。 请通过我的代码。 最后一次点击单选按钮

完成插入
package com.tmm.android.chuck;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.tmm.android.chuck.db.DBAdapter;
import com.tmm.android.chuck.quiz.GamePlay;
import com.tmm.android.chuck.quiz.Question;
import com.tmm.android.chuck.util.Utility;

/**
 * @author Sanket Naik
 * 
 */
public class QuestionActivity extends Activity implements
        OnCheckedChangeListener {

    private Question currentQ;
    final DBAdapter db = new DBAdapter(this);
    private GamePlay currentGame;
    Calendar C = Calendar.getInstance();
    String text, topic1;
    String mon = C.get(Calendar.MONTH) + 1 + "";
    static int a = 0;
    ArrayList<String> getselect = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);

        // Bundle b = getIntent().getExtras();
        if (a < 1) {

            topic1 = getIntent().getStringExtra("itemname");

            Log.i("sanket", topic1);
        }
        /**
         * Configure current game and get question
         */
        currentGame = ((ChuckApplication) getApplication()).getCurrentGame();
        currentQ = currentGame.getNextQuestion();

        RadioGroup rdgb = (RadioGroup) findViewById(R.id.group1);
        rdgb.setOnCheckedChangeListener(this);
        /**
         * Update the question and answer options..
         */
        setQuestions();

    }

    private void setQuestions() {
        // set the question text from current question
        String question = Utility.capitalise(currentQ.getQuestion()) + "?";
        TextView qText = (TextView) findViewById(R.id.question);
        qText.setText(question);

        // set the available options
        List<String> answers = currentQ.getQuestionOptions();
        TextView option1 = (TextView) findViewById(R.id.answer1);
        option1.setText(Utility.capitalise(answers.get(0)));

        TextView option2 = (TextView) findViewById(R.id.answer2);
        option2.setText(Utility.capitalise(answers.get(1)));

        TextView option3 = (TextView) findViewById(R.id.answer3);
        option3.setText(Utility.capitalise(answers.get(2)));

        TextView option4 = (TextView) findViewById(R.id.answer4);
        option4.setText(Utility.capitalise(answers.get(3)));
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (a > 0) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);

    }

    private boolean checkAnswer() {
        String answer = getSelectedAnswer();
        if (answer == null) {

            return false;
        } else {

            if (currentQ.getAnswer().equalsIgnoreCase(answer)) {

                currentGame.incrementRightAnswers();
            } else {

                currentGame.incrementWrongAnswers();
            }

            return true;
        }
    }

    /**
     * 
     */
    public String getSelectedAnswer() {
        RadioButton c1 = (RadioButton) findViewById(R.id.answer1);
        RadioButton c2 = (RadioButton) findViewById(R.id.answer2);
        RadioButton c3 = (RadioButton) findViewById(R.id.answer3);
        RadioButton c4 = (RadioButton) findViewById(R.id.answer4);
        if (c1.isChecked()) {
            return c1.getText().toString();

        }
        if (c2.isChecked()) {
            return c2.getText().toString();
        }
        if (c3.isChecked()) {
            return c3.getText().toString();
        }
        if (c4.isChecked()) {
            return c4.getText().toString();
        }

        return null;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        a++;

        if (!checkAnswer())
            return;

        /**
         * check if end of game
         */
        if (currentGame.isGameOver()) {

            try {
                db.open();
                String total = currentGame.getRight() + "";
                db.insertOptions(topic1, total, mon);
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Intent i = new Intent(this, EndgameActivity.class);
            startActivity(i);
            a = 0;
            finish();
        } else {
            Intent i = new Intent(this, QuestionActivity.class);
            startActivity(i);
            finish();
        }
    }

}

插入正在进行但是没有插入topic1的值,正在插入mon,total的值。只是表的第一列是空白 但同时在OnCreate()中,当我打印topic1的值时,它正在显示

1 个答案:

答案 0 :(得分:0)

总是使用putExtra将值发送到另一个活动:

Intent i = new Intent(this, EndgameActivity.class);
i.putExtra("itemname", Value) // Value can be almost of any type
startActivity(i);
finish();

检查一下: http://developer.android.com/reference/android/content/Intent.html