Pra*_*dhi 1 java android android-intent
我正在尝试制作测验Android应用程序.我正在尝试将用户出错的问题和多项选择显示在另一项活动中.我不知道如何转移多个错误的问题.
这是我的测验java类.
public class QuizActivityMarketing extends Activity{
private Iterator<Question> questionIterator;
private TextView txtQuestion;
private RadioButton rda, rdb, rdc, rdd;
public Button butNext;
private int qid = 0;
private Question currentQ;
private int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
// Define your views
txtQuestion = (TextView) findViewById(R.id.textView1);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
rdd = (RadioButton) findViewById(R.id.radio3);
butNext = (Button) findViewById(R.id.button1);
//Log question and option if wrong
TextView question = (TextView) findViewById(R.id.textView1);
RadioButton optA = (RadioButton) findViewById(R.id.radio0);
RadioButton optB = (RadioButton) findViewById(R.id.radio1);
RadioButton optC = (RadioButton) findViewById(R.id.radio2);
RadioButton optD = (RadioButton) findViewById(R.id.radio3);
if (!currentQ.getANSWER().trim().equals(answer.getText())) {
Log.d("Current Question", ""+question.getText());
Log.d("OptionA", ""+optA.getText());
Log.d("OptionB", ""+optB.getText());
Log.d("OptionC", "" + optC.getText());
Log.d("OptionD", "" + optD.getText());
}
// Load the next question, if there are any
if (questionIterator.hasNext()) {
currentQ = questionIterator.next();
setQuestionView(currentQ);
qid++;
}
// Done asking questions
Intent intent1 = getIntent();
int buttonValue = Integer.valueOf(intent1.getExtras().getString("button"));
if (qid > buttonValue)
{
Run Code Online (Sandbox Code Playgroud)
这是我的Result类
public class ResultActivityMarketing extends AppCompatActivity {
//get text view for score
TextView testResult = (TextView) findViewById(R.id.testResult);
//get score
Bundle extras = getIntent().getExtras();
if (extras != null) {
int score = extras.getInt("score");
int numberquestions = extras.getInt("button");
testResult.setText("You got " + score + " out of " + numberquestions + " questions correct");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是结果活动的XML类.
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/wrong_questions_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Wrong Questions"
android:paddingTop="10dp"/>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginTop="30dp"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton"/>
</RadioGroup>
</FrameLayout>
</ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗.我真的很感激.我不知道该怎么做.提前致谢!
首先创建一个ArrayList类变量来保存失败的问题
private ArrayList<QuestionFialed> failedQuestions;
Run Code Online (Sandbox Code Playgroud)
将每个失败的问题添加到QuestionFialed类的对象中,该类添加到failedQuestions arraylist中.
if (isAnswerWrong) {
failedQuestions.add(new QuestionResult(questionNumber, selectedAnswer, correctAnswer));
}
Run Code Online (Sandbox Code Playgroud)
然后在测验结束时,您可以将包含失败问题的ArrayList发送到另一个Activity
if (isGameOver) {
Intent intent = new Intent(this, QuizResultActivity.class);
intent.putParcelableArrayListExtra("FailedResult", failedQuestions);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
在下一个活动页面上,您可以像这样返回您的ArrayList
ArrayList<QuestionResult> myFailedQuestions = getIntent().getParcelableArrayListExtra("FailedResult");
Run Code Online (Sandbox Code Playgroud)
我希望这会指出你正确的方向
更新您的特定需求
我看不到你的整个代码,但这是如何在你的应用程序中适应它
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class QuizActivityMarketing extends AppCompatActivity {
private Iterator<Question> questionIterator;
private TextView txtQuestion;
private RadioButton rda, rdb, rdc, rdd;
private RadioGroup radioGroup;
public Button butNext;
private int qid = 0;
private Question currentQ;
private int score = 0;
// store failed questions and correct answer
private ArrayList<QuestionFailed> failedQuestions;
// holds currently selected user answer
private String currentSelectedAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_activity_marketing);
// create object of a ArrayList and clear the ArrayList object
failedQuestions = new ArrayList<QuestionFailed>();
failedQuestions.clear();
// Define your views
txtQuestion = (TextView) findViewById(R.id.textView1);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
rdd = (RadioButton) findViewById(R.id.radio3);
// RadioGroup
// select the current RadioButton selected
radioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radio0){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio0)).getText().toString();
}
else if(checkedId == R.id.radio1){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio1)).getText().toString();
}
else if(checkedId == R.id.radio2){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio2)).getText().toString();
}
else if(checkedId == R.id.radio3){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio3)).getText().toString();
}
else{
currentSelectedAnswer = "";
}
}
});
// use button event to move to next question if there is more
butNext = (Button) findViewById(R.id.button1);
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check if the user selected result matches correct answer or not
if(currentSelectedAnswer.equals(currentQ.getANSWER().trim())){
// answer is correct
}else{
//incorrect answer selected
// then add failed answer in the QuestionFailed object inside ArrayList
failedQuestions.add(new QuestionResult(qid, currentSelectedAnswer, currentQ.getANSWER()));
}
// check if there is more questions
if (questionIterator.hasNext()) {
currentQ = questionIterator.next();
//Display next questions
setQuestionView(currentQ);
//increment question number
qid++;
}
else{
// There is no question left which means that the quiz is over
// send the failed answer to next activity
Intent intent = new Intent(this, ResultActivityMarketing.class);
intent.putParcelableArrayListExtra("FailedResult", failedQuestions);
startActivity(intent);
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
QuestionFailed类实现了Parcelable.看课
import android.os.Parcel;
import android.os.Parcelable;
public class QuestionFailed implements Parcelable {
private int questionId;
private String yourAnswer;
private String correctAnswer;
public QuestionFailed(int questionId, String yourAnswer, String correctAnswer) {
this.questionId = questionId;
this.yourAnswer = yourAnswer;
this.correctAnswer = correctAnswer;
}
public QuestionFailed(Parcel in) {
questionId = in.readInt();
yourAnswer = in.readString();
correctAnswer = in.readString();
}
public int getQuestionId() {
return questionId;
}
public String getYourAnswer() {
return yourAnswer;
}
public String getCorrectAnswer() {
return correctAnswer;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(questionId);
dest.writeString(yourAnswer);
dest.writeString(correctAnswer);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator<QuestionFailed>() {
public QuestionFailed createFromParcel(Parcel in) {
return new QuestionFailed(in);
}
public QuestionFailed[] newArray(int size) {
return new QuestionFailed[size];
}
};
}
Run Code Online (Sandbox Code Playgroud)
对于结果活动,您将获得失败的问题计数并使用它来计算得分.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
public class ResultActivityMarketing extends AppCompatActivity {
public static final int totalQuizCount = 30;
private ArrayList<QuestionFailed> myFailedQuestions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_activity_marketing);
myFailedQuestions = getIntent().getParcelableArrayListExtra("FailedResult");
int failedCount = myFailedQuestions.size();
// get the score
int score = 30 - failedCount;
// Display the result
TextView testResult = (TextView) findViewById(R.id.testResult);
testResult.setText("You got " + String.valueOf(score) + " out of " + String.valueOf(numberquestions) + " questions correct");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
324 次 |
| 最近记录: |