Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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 为下一个活动获取多个值_Java_Android_Sqlite - Fatal编程技术网

Java 为下一个活动获取多个值

Java 为下一个活动获取多个值,java,android,sqlite,Java,Android,Sqlite,应用程序活动将从sqlite数据库中随机获得10个问题。我想获得下一个活动的问题和答案,以显示答案。我认为它应该返回“NextQ”按钮上的值。如何设置 public class QuizGram extends Activity { private SQLiteDatabase database; private static final String DB_NAME = "eng.db"; private static final String TABLE_NAME

应用程序活动将从sqlite数据库中随机获得10个问题。我想获得下一个活动的问题和答案,以显示答案。我认为它应该返回“NextQ”按钮上的值。如何设置

public class QuizGram extends Activity {

    private SQLiteDatabase database;
    private static final String DB_NAME = "eng.db";
    private static final String TABLE_NAME = "quiz";
    private static final String QUIZ_ID = "_id";
    private static final String QUIZ_TYPE = "quiz_type";
    private static final String QUESTION = "question";
    private static final String A = "a";
    private static final String B = "b";
    private static final String C = "c";
    private static final String ANS = "ans";

    List<Question> quesList;
    int score=0;
    int qid=0;
    Question currentQ;
    TextView Questions;
    RadioButton Radio0, Radio1, Radio2;
    Button NextQ;

    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.quizdetail);

        DBhelper dbHelper = new DBhelper(this, DB_NAME);
        database = dbHelper.openDataBase();
        quesList = this.getQuestions();
        currentQ = quesList.get(qid);
        Questions = (TextView)findViewById(R.id.questions);
        Radio0 = (RadioButton)findViewById(R.id.radio0);
        Radio1 = (RadioButton)findViewById(R.id.radio1);
        Radio2 = (RadioButton)findViewById(R.id.radio2);
        NextQ = (Button)findViewById(R.id.nextQ);
        setQuestionView();
        NextQ.setOnClickListener(new View.OnClickListener() {       
            @Override
            public void onClick(View v) {
                RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
                RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
                Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
                if(currentQ.getANSWER().equals(answer.getText()))
                {
                    score++;
                    Log.d("score", "Your score"+score);
                }
                if(qid<10){                 
                    currentQ=quesList.get(qid);
                    setQuestionView();
                }else{
                    Intent intent = new Intent(QuizGram.this, QuizResult.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("score", score); //Your score
                    intent.putExtras(bundle); //Put your score to your next Intent
                    startActivity(intent);
                    finish();
                }
            }
        });
    }


    public List<Question> getQuestions() 
    {
         List<Question> quesList = new ArrayList<Question>();
        //db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
         Cursor cursor = database.query(TABLE_NAME,
                 new String[] {QUIZ_ID, QUIZ_TYPE, QUESTION, A, B, C, ANS},
                 QUIZ_TYPE + "='grammar'" ,
                 null, null, null, "RANDOM()", "10");
         // looping through all rows and adding to list
         if (cursor.moveToFirst()) {
            do {
                Question quest = new Question();
                quest.setID(cursor.getInt(0));
                quest.setTYPE(cursor.getString(1));
                quest.setQUESTION(cursor.getString(2));
                quest.setOPTA(cursor.getString(3));
                quest.setOPTB(cursor.getString(4));
                quest.setOPTC(cursor.getString(5));
                quest.setANSWER(cursor.getString(6));
                quesList.add(quest);
                } while (cursor.moveToNext());
         }
         // return quest list
         return quesList;
    }

    private void setQuestionView()
    {
        Questions.setText(currentQ.getQUESTION());
        Radio0.setText(currentQ.getOPTA());
        Radio1.setText(currentQ.getOPTB());
        Radio2.setText(currentQ.getOPTC());
        qid++;
     }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Intent myIntent = new Intent();
            myIntent = new Intent(QuizGram.this, Quiz.class);
            startActivity(myIntent);
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }

}
公共类QuizGram扩展活动{
专用数据库;
私有静态最终字符串DB_NAME=“eng.DB”;
私有静态最终字符串表\u NAME=“quick”;
私有静态最终字符串测验\u ID=“\u ID”;
私有静态最终字符串测验\u TYPE=“测验\u TYPE”;
私有静态最终字符串QUESTION=“QUESTION”;
私有静态最终字符串A=“A”;
私有静态最终字符串B=“B”;
私有静态最终字符串C=“C”;
私有静态最终字符串ANS=“ANS”;
清单问题清单;
智力得分=0;
int-qid=0;
问题Q;
文本视图问题;
RadioButton Radio0、Radio1、Radio2;
按钮NextQ;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.quizdetail);
DBhelper DBhelper=新的DBhelper(这个,DB_名称);
database=dbHelper.openDataBase();
quesList=this.getQuestions();
currentQ=quesList.get(qid);
问题=(TextView)findViewById(R.id.Questions);
Radio0=(RadioButton)findViewById(R.id.Radio0);
Radio1=(RadioButton)findViewById(R.id.Radio1);
Radio2=(RadioButton)findViewById(R.id.Radio2);
NextQ=(按钮)findviewbyd(R.id.NextQ);
setQuestionView();
NextQ.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
放射组grp=(放射组)findViewById(R.id.RadioGroup 1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d(“yourans”,currentQ.getANSWER()+“”+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
分数++;
Log.d(“分数”、“你的分数”+分数);
}

如果(qid非常简单,只需在您想要进行下一个活动的地方单击功能,然后定义它即可

String strMessage = yourEdittext.getText().toString());
        String strMessage1 = yourEdittext1.getText().toString());
        String strMessage2 = yourEdittext2.getText().toString());
    // so on you can directly set values
    // you have to just set values in string                      

      Intent i = new Intent(getApplicationContext(), nextactivity.class)   
      i.putExtra("new_variable_name",strMessage);
      i.putExtra("new_variable_name1",strMessage1);     
      i.putExtra("new_variable_name2",strMessage2);

      startActivity(i);  
然后在下一个活动中通过

Bundle extras = getIntent().getExtras();
String var="";
String var1="";
String var2="";
      if (extras != null)
      {

        String value= extras.getString("new_variable_name");       
        value= var;
        String value1= extras.getString("new_variable_name1");
        value1= var1;  

         String value2= extras.getString("new_variable_name2");
         value2= var;
      }

1.您可以通过将问题对象传递给intent来实现这一点。 在发送活动中->

Bundle bundle = getIntent().getExtras();
Question q= (Question) bundle.getSerializable("Question");
intent.putExtra(“问题”,currentQ);

在接收活动中->

Bundle bundle = getIntent().getExtras();
Question q= (Question) bundle.getSerializable("Question");
确保为问题类实现Serializable


2.另一个选项是通过intent传递单个值

,但该活动有10个问题,我需要每次单击下一个问题获取值,所以我需要获取值10次。也许可以创建一个数组来存储它?只需获取10个变量…我不知道我们是否可以在intent中将数组作为变量传输。非常感谢。我想我正在尝试你的方法应该是工作。