i am creating a quiz app that is multiple choice
в моем первом задании (MainActivity) - вопрос (текст) и варианты выбора
, а во втором задании (QuizImage1) - вопрос (изображение) и варианты выбора
как Получу ли я результат оценки в основной деятельности, чтобы просмотреть также в викторине1
![mainactivity](https://i.stack.imgur.com/pLgfE.jpg)
![second activity](https://i.stack.imgur.com/aUaO9.jpg)
второе изображение - моя ошибка на quizimage1, потому что я не скопировал первый счет активности
надеюсь, вы понимаете, о чем я говорю
plss исправьте мои коды
iam новичок в java
Main_Activity.java
package com.example.ltoexam;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private com.example.ltoexam.QuestionLibrary nQuestionLibrary = new com.example.ltoexam.QuestionLibrary();
public TextView nScoreView;
private TextView nQuestionView;
private Button nButtonChoice1;
private Button nButtonChoice2;
private Button nButtonChoice3;
private Button quit;
private String nAnswer;
public int nScore = 0;
private int nQuestionNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nScoreView = (TextView) findViewById(R.id.score);
nQuestionView = (TextView) findViewById(R.id.question);
nButtonChoice1 = (Button) findViewById(R.id.choice1);
nButtonChoice2 = (Button) findViewById(R.id.choice2);
nButtonChoice3 = (Button) findViewById(R.id.choice3);
quit = (Button) findViewById(R.id.quit) ;
updateQuestion();
//Start of Button Listener for Button1
nButtonChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//My logic for Button goes in here
if (nButtonChoice1.getText() == nAnswer){
nScore =nScore + 1;
updateScore(nScore);
updateQuestion();
//This line of code is optional
Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "wrong the answer is " + nAnswer, Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button2
//Start of Button Listener for Button2
nButtonChoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//My logic for Button goes in here
if (nButtonChoice2.getText() == nAnswer){
nScore =nScore + 1;
updateScore(nScore);
updateQuestion();
//This line of code is optional
Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "wrong the answer is " + nAnswer, Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
quit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
//End of Button Listener for Button2
//Start of Button Listener for Button3
nButtonChoice3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//My logic for Button goes in here
if (nButtonChoice3.getText() == nAnswer){
nScore =nScore + 1;
updateScore(nScore);
updateQuestion();
//This line of code is optional
Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "wrong the answer is " + nAnswer, Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button2
}
private void updateQuestion(){
if(nQuestionNumber==7){
openQuizImage1();}
else {
nQuestionView.setText(nQuestionLibrary.getQuestion(nQuestionNumber));
nButtonChoice1.setText(nQuestionLibrary.getChoice1(nQuestionNumber));
nButtonChoice2.setText(nQuestionLibrary.getChoice2(nQuestionNumber));
nButtonChoice3.setText(nQuestionLibrary.getChoice3(nQuestionNumber));
nAnswer = nQuestionLibrary.getCorrectAnswer(nQuestionNumber);
nQuestionNumber++;
}
}
public void updateScore(int point){
nScoreView.setText("" + nScore);
}
public void openQuizImage1() {
Intent intent = new Intent(this, QuizImage1.class);
startActivity(intent);
}
}
QuestionLibrary.java
package com.example.ltoexam;
public class QuestionLibrary {
private String nQuestions [] = {
"1.The three colors of the traffic lights are:",
"2.Yellow triangular signs provide what kind of information",
"3.Which of the following traffic signs are blue?",
"4.Steady green light means",
"5.A flashing yellow light at a road crossing signifies",
"6.A solid white line on the right edge of the highway slopes in towards your left. This shows that",
"7.You are in a No-Passing zone when the center of the road is marked by"
};
private String nChoices [] [] = {
{"red, green and yellow", "red, green and blue", "yellow, green and blue"},
{"warning", "hospital across", "speed limit"},
{"regulatory signs", "information signs", "danger warning signs"},
{"you must yield to all pedestrians and other motorists using the intersection", "go, it is safe to do so", "proceed cautiously through the intersection before the light changes to red."},
{"Caution - slow down and proceed with caution", "Stop and stay until light stops flashing", "Wait for the green light"},
{"there is an intersection joint ahead", "the road will get narrower", "you are approaching a construction area"},
{"a broken yellow line","a broken white line","two solid yellow lines"}
};
private String nCorrectAnsers[] = {"red, green and yellow", "warning", "information signs", "go, it is safe to do so", "Caution - slow down and proceed with caution", "the road will get narrower", "two solid yellow lines"};
public String getQuestion(int a) {
String question = nQuestions[a];
return question;
}
public String getChoice1(int a) {
String choice0 = nChoices[a] [0];
return choice0;
}
public String getChoice2(int a) {
String choice1 = nChoices[a] [1];
return choice1;
}
public String getChoice3(int a) {
String choice2 = nChoices[a] [2];
return choice2;
}
public String getCorrectAnswer(int a) {
String answer = nCorrectAnsers[a];
return answer;
}
}
QuizImage1.java (this is my second activity)
package com.example.ltoexam;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizImage1 extends AppCompatActivity {
private Button but1;
private Button but2;
private Button but3;
public TextView nScoreView;
public int nScore;
public TextView nScoreView() {
return nScoreView;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_image1);
but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
but3 = (Button) findViewById(R.id.but3);
nScoreView = (TextView) findViewById(R.id.score);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(QuizImage1.this, "wrong the answer is Road goes left or right", Toast.LENGTH_SHORT).show();
openQuizImage2();
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(QuizImage1.this, "wrong the answer is Road goes left or right", Toast.LENGTH_SHORT).show();
openQuizImage2();
}
});
but3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(QuizImage1.this, "correct", Toast.LENGTH_SHORT).show();
openQuizImage2();
nScore =nScore + 1;
updateScore(nScore);
}
});
}
public void updateScore(int point){
nScoreView.setText("" + nScore);}
public void openQuizImage2(){
Intent intent = new Intent(this, QuizImage2.class);
startActivity(intent);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginBottom="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:id="@+id/score_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_alignParentRight="true"
android:text="0"
android:id="@+id/score"/>
</RelativeLayout>
<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="116dp"
android:layout_marginBottom="40dp"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="Which thing is alive?"
android:textSize="20sp"
android:textStyle="bold|italic" />
<Button
android:id="@+id/choice1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#0091EA"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="bird"
android:textColor="#fff" />
<Button
android:id="@+id/choice2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#0091EA"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="door"
android:textColor="#fff" />
<Button
android:id="@+id/choice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#0091EA"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="rock "
android:textColor="#fff" />
<Button
android:id="@+id/quit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#871C1C"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="Quit"
android:textColor="#fff" />
</LinearLayout>
activity_quiz_image1.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".QuizImage1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginBottom="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:id="@+id/score_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_alignParentRight="true"
android:text="0"
android:id="@+id/score"/>
</RelativeLayout>
<Button
android:id="@+id/but1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="317dp"
android:layout_marginBottom="238dp"
android:background="#0091EA"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="road ends"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/but2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="396dp"
android:layout_marginBottom="159dp"
android:background="#0091EA"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="crossroads"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/but3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="476dp"
android:layout_marginBottom="79dp"
android:background="#0091EA"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="Road goes left or right"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/quit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="549dp"
android:layout_marginBottom="6dp"
android:background="#871C1C"
android:fontFamily="@font/carter_one"
android:padding="8dp"
android:text="Quit"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="249dp"
android:layout_height="182dp"
app:srcCompat="@drawable/road_goes_left_right_road_traffic_sign"
tools:layout_editor_absoluteX="81dp"
tools:layout_editor_absoluteY="67dp" />
</androidx.constraintlayout.widget.ConstraintLayout>