Help guys to figure it out. I am new to programming. I make an application test. There is a first activity, in it there is a button-menu, you click and go to the second activity, where questions with answers are displayed. I take all the content from txt-files that are in the assets folder. Now there are 7 of them. One for each question.
The files have the following names: z1v1.txt - task one question one, the second z1v2, etc.
String parsing_txt = "z1v" + actualq + ".txt"; The actualq variable - the actual question by clicking on the radiobutton is incremented by 1, and so the parsing happens and the questions change.
When I sparse the last question, I throw it into 1 activity. Question: Why? And the second: how to write a check, such as the last TXT - the output of results.
I suffered, nothing comes out. I already increased the actualq to 7, for example, on the seventh press, bring me my method of calculating the results (7 because the first question is displayed in onCreate)
if (actualq==7){ displayResult(); } All the best. Throws in the activity with the menu button and even if you crack.
Here is the code:
public class MainActivityTwo extends Activity implements OnClickListener { Button next, resalt; TextView question; TextView right_or_wrong; int total = 7;// сколько всего вопросов в тесте int right_option = 0; // правильные ответы int actualq = 1;// номер текущего вопроса int actuala = 0;// номер текущего otveta (1-pervaja knopka, 2-vtoraja knopka, 3-tretja knopka) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_two); resalt = (Button) findViewById(R.id.resalt); resalt.setOnClickListener(this); question = (TextView) findViewById(R.id.textView1); right_or_wrong = (TextView) findViewById(R.id.textView3); String parsing_txt = "z1v1.txt";// String s1 = LoadTXT.TXT(this, parsing_txt);// записываем содержимое файла в переменную типа string StringTokenizer st1 = new StringTokenizer(s1, "*");// делим строку по критерию * question.setText(st1.nextToken()); st1.nextToken(); String actuala1 = st1.nextToken();// записываем ответы actuala = Integer.parseInt(actuala1);// ответы из string в примитивный тип int st1.nextToken(); String Are = st1.nextToken(); st1.nextToken(); String Is = st1.nextToken(); st1.nextToken(); String Am = st1.nextToken(); RadioButton option1 = (RadioButton) findViewById(R.id.variant1);// находим radiobutton по id option1.setText(Are);// выводим 1 вариант ответа. RadioButton option2 = (RadioButton) findViewById(R.id.variant2);// находим radiobutton по id option2.setText(Is);// выводим 2 вариант ответа. RadioButton option3 = (RadioButton) findViewById(R.id.variant3);// находим radiobutton по id option3.setText(Am);// выводим 3 вариант ответа. }// end method onCreate @Override public void onClick(View v) { RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radio_group); int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId())); /*if (actualq==7){ displayResult(); }*/ if (index == -1) { Toast toast = Toast.makeText(this, "Вы ничего не выбрали", Toast.LENGTH_SHORT); toast.show(); }// проверка нажал ли юзер вообще на radiobutton */ if (index == 0) { /*1 knopka*/ if (actuala == 1) { Toast toast = Toast.makeText(this, "pravilny otvet", Toast.LENGTH_SHORT); toast.show(); right_option++; actualq++; } else { Toast toast = Toast.makeText(this, "ne pravilny otvet", Toast.LENGTH_SHORT); toast.show(); actualq++; } } if (index == 1) { /*2 knopka*/ if (actuala == 2) { Toast toast = Toast.makeText(this, "pravilny otvet", Toast.LENGTH_SHORT); toast.show(); right_option++; actualq++; } else { Toast toast = Toast.makeText(this, "ne pravilny otvet", Toast.LENGTH_SHORT); toast.show(); actualq++; } } if (index == 2) { /*2 knopka*/ if (actuala == 3) { Toast toast = Toast.makeText(this, "pravilny otvet", Toast.LENGTH_SHORT); toast.show(); right_option++; actualq++; } else { Toast toast = Toast.makeText(this, "ne pravilny otvet", Toast.LENGTH_SHORT); toast.show(); actualq++; } } String parsing_txt = "z1v" + actualq + ".txt";// z1v1 String s1 = LoadTXT.TXT(this, parsing_txt);// записываем содержимое файла в переменную типа string StringTokenizer st1 = new StringTokenizer(s1, "*"); question.setText(st1.nextToken());// выводим вопросы теста st1.nextToken(); String actuala1 = st1.nextToken();// записываем правильные ответы в string переменную actuala = Integer.parseInt(actuala1);// ответы из string в тип int st1.nextToken(); String Are = st1.nextToken();// radiobutton st1.nextToken(); String Is = st1.nextToken();// radiobutton st1.nextToken(); String Am = st1.nextToken();// radiobutton RadioButton option1 = (RadioButton) findViewById(R.id.variant1);// находим radiobutton по id option1.setText(Are);// выводим 1 вариант ответа. RadioButton option2 = (RadioButton) findViewById(R.id.variant2);// находим radiobutton по id option2.setText(Is);// выводим 2 вариант ответа. RadioButton option3 = (RadioButton) findViewById(R.id.variant3);// находим radiobutton по id option3.setText(Am);// выводим 3 вариант ответа. myRadioGroup.clearCheck();// снимаем метку }// end method onClick void displayResult() {// выводить результаты я буду как-то так TextView resultTV = (TextView) findViewById(R.id.textView3); resultTV.setText("Тест завершён! Правильно: " + right_option + " из " + total + "."); } }// end class MainActivityTwo