Dear developers. I am writing my first full-fledged application on android, something like a text game. Faced with a little problem.

There is a TextView, when clicked, the following text is displayed. When text with a specific index appears, a list of response options is displayed (two more TextViews).

The problem is that after clicking on one of the answer options I can’t define another array of strings for the main TextView.

Maybe I wrote it in an incomprehensible language, and the code itself is probably not very good, but I will be glad to help. Thank you in advance.

mMainTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //индСксация массива mCurrentIndex = (mCurrentIndex + 1) % mainTextArray.meetingTextsArray.length; //массив строк с ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ΠΌ индСкса ΠΏΡ€ΠΈ ΠΊΠ»ΠΈΠΊΠ΅ int text = mainTextArray.meetingTextsArray[mCurrentIndex].getTextResId(); mMainTextView.setText(text); //ΠΏΡ€ΠΈ Π½ΡƒΠΆΠ½ΠΎΠΌ индСксС Π²ΡΠΏΠ»Ρ‹Π²Π°ΡŽΡ‚ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚Ρ‹ ΠΎΡ‚Π²Π΅Ρ‚Π°(Π΅Ρ‰Ρ‘ 2 textview) if(mCurrentIndex == 1) { showChoiceButtons(R.string.meeting_left_text, R.string.meeting_right_text, mMainTextView); leftChoiceButtonPressed(); } } }); } private void leftChoiceButtonPressed() { //ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚ ΠΎΡ‚Π²Π΅Ρ‚Π°, Π·Π°Ρ‚Π΅ΠΌ Π½ΡƒΠΆΠ½ΠΎ ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ массив строк для основного //textview mLeftChoiceTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mMainTextView.setText("ВСстим"); } }); } private void showChoiceButtons(int leftButton, int rightButton, View view) { //ΠΏΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ Π²ΡΠΏΠ»Ρ‹Π²Π°ΡŽΡ‰ΠΈΠ΅ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚Ρ‹ ΠΎΡ‚Π²Π΅Ρ‚Π° mLeftChoiceTextView.setVisibility(View.VISIBLE); mRightChoiceTextView.setVisibility(View.VISIBLE); mLeftChoiceTextView.setText(leftButton); mRightChoiceTextView.setText(rightButton); view.setClickable(false); } <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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" android:background="@drawable/background" tools:context=".BookActivity" tools:layout_editor_absoluteY="81dp"> <TextView android:id="@+id/main_text" android:layout_width="218dp" android:layout_height="217dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="144dp" android:gravity="center_horizontal|center_vertical" android:textSize="15sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/left_choice_text" android:layout_width="114dp" android:layout_height="113dp" android:layout_marginBottom="16dp" android:layout_marginStart="32dp" android:textSize="15sp" android:textStyle="bold" android:textColor="@color/light" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/right_choice_text" android:layout_width="114dp" android:layout_height="113dp" android:layout_marginBottom="16dp" android:layout_marginEnd="32dp" android:textSize="15sp" android:textStyle="bold" android:textColor="@color/light" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> 

  • "After clicking on one of the answer options, I cannot determine another array of strings for the main TextView." And in words if, after clicking on mLeftChoiceTextView what next do you want to happen? so that in mMainTextView when clicked, the mainTextArray.meetingTextsArray array mainTextArray.meetingTextsArray changed to another? - zayn1991
  • Yes exactly. - Geronimo

1 answer 1

If I understand correctly, you want to change the array, the text of the main textView and the text on the other 2 textView when you click on, for example, the left textView .

I would do this:

 changeTextArrays(0); // 0 - Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ mMainTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ΠΎΡ‚ΡΡŽΠ΄Π° пСрСнСсСно нСсколько строк Π² ΠΌΠ΅Ρ‚ΠΎΠ΄ changeTextArrays //ΠΏΡ€ΠΈ Π½ΡƒΠΆΠ½ΠΎΠΌ индСксС Π²ΡΠΏΠ»Ρ‹Π²Π°ΡŽΡ‚ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚Ρ‹ ΠΎΡ‚Π²Π΅Ρ‚Π°(Π΅Ρ‰Ρ‘ 2 textview) if(mCurrentIndex == 1) { showChoiceButtons(R.string.meeting_left_text, R.string.meeting_right_text, mMainTextView); //leftChoiceButtonPressed(); // Ρ‚ΡƒΡ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ устанавливаСтся дСйствиС ΠΊΠ»ΠΈΠΊΠ°. ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ Π΅Π³ΠΎ ΠΌΠΎΠΆΠ½ΠΎ Π²Ρ‹Π·Π²Π°Ρ‚ΡŒ 1 Ρ€Π°Π·, поэтому написал послС mMainTextView.setOnClickListener } } }); leftChoiceButtonPressed(); } private void leftChoiceButtonPressed() { mLeftChoiceTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mMainTextView.setText("ВСстим");// помСняли Π³Π»Π°Π²Π½Ρ‹ΠΉ тСкст, Π΄Π°? //ΠΏΠΎ Π»ΠΎΠ³ΠΈΠΊΠ΅ Π½Π°Π΄ΠΎ Π΅Ρ‰Ρ‘ названия Π»Π΅Π²ΠΎΠΉ ΠΈ ΠΏΡ€Π°Π²ΠΎΠΉ тСкстов ΠΏΠΎΠΌΠ΅Π½ΡΡ‚ΡŒ Π½Π° Ρ‚Π΅, //Ρ‡Ρ‚ΠΎ Π΄ΠΎΠ»ΠΆΠ½Ρ‹ ΠΎΡ‚ΠΎΠ±Ρ€Π°Π·ΠΈΡ‚ΡŒΡΡ ΠΏΡ€ΠΈ Π²Ρ‹Π±ΠΎΡ€Π΅ Π»Π΅Π²ΠΎΠ³ΠΎ ΠΊΠ»ΠΈΠΊΠ°: showChoiceButtons(R.string.left_text_Π½ΠΎΠ²Ρ‹ΠΉ, R.string.right_text_Π½ΠΎΠ²Ρ‹ΠΉ, mMainTextView); //Π½Π° счёт смСны массива. ΠŸΡ€Π΅Π΄Π»ΠΎΠΆΡƒ Ρ‚Π°ΠΊΠΎΠΉ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚: changeTextArrays(1); // Π² Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π°Ρ… ΠΊ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρƒ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ условиС для смСны массива } }); } // этот ΠΌΠ΅Ρ‚ΠΎΠ΄ ΠΌΠΎΠΆΠ½ΠΎ Π²Ρ‹Π·Π²Π°Ρ‚ΡŒ Π΄ΠΎ mMainTextView.setOnClickListener(... // Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ я пСрСнёс строки с индСксами ΠΈΠ· mMainTextView.setOnClickListener private void changeTextArrays(int type){ // Ρƒ нас Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹ΠΉ массив, ΠΊΡƒΠ΄Π° Π±ΡƒΠ΄Π΅ΠΌ ΠΏΡ€ΠΈΡΠ²Π°ΠΈΠ²Π°Ρ‚ΡŒ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ массивы Π² зависимости ΠΎΡ‚ условий (Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, type) // tempTextArray - массив, с Ρ‚ΠΈΠΏΠΎΠΌ, ΠΊΠ°ΠΊ Ρƒ mainTextArray if(type == 0){ tempTextArray = mainTextArray; // Π½Π°Ρ‡Π°Π»ΡŒΠ½Ρ‹ΠΉ массив, Π±Π΅Π· ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ } else if(type == 1){ //Ρ‚ΡƒΡ‚ мСняСм массив tempTextArray = mainTextArray2; // массив, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π΄ΠΎΠ»ΠΆΠ΅Π½ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ послС ΠΊΠ»ΠΈΠΊΠ° } else if(type == 2){ //Ρ‚ΡƒΡ‚ мСняСм массив tempTextArray = mainTextArray3; // массив 3ΠΉ ΠΈ Ρ‚Π°ΠΊ Π΄Π°Π»Π΅Π΅ } //индСксация массива mCurrentIndex = (mCurrentIndex + 1) % tempTextArray.meetingTextsArray.length; //массив строк с ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ΠΌ индСкса ΠΏΡ€ΠΈ ΠΊΠ»ΠΈΠΊΠ΅ int text = tempTextArray.meetingTextsArray[mCurrentIndex].getTextResId(); mMainTextView.setText(text); } 

But this example also needs improvement, for example, clicking on the left textView will call a method with the same value changeTextArrays(1); therefore, you can replace the number 1 with a global variable, which will also change, as well as the array in the changeTextArrays method

  • Thank you very much! You understood correctly. Your amendments will help a lot! - Geronimo
  • @Geronimo if the answer was more useful than the rest, in the future, do not forget to mark it as correct. Check it out. Plus your rating for your diligence in programming). Also, if you need additional help on this issue, ask - zayn1991