Cześć. Chcę aby po każdym dotknięciu/kliknięciu przycisku nextButton losowany był numer indeksu n i następnie z tablic englishWords i polishWords wyciągamy słowa o tym właśnie indeksie i dodatkowo numer indeksu dodajemy do listy notAvailableIndexes, po to żeby nie dublować później losowanych słów. Jednym z problemów jest np. to, że po kliknięciu przycisku losowanie odbywa się tak jakby dwa razy, teksty dwóch TextView są w krótkim czasie aktualizowane dwa razy (po jednym kliknięciu). Co poprawić w poniższym kodzie?
LearnGreetingsActivity.java
package com.blogspot.dammarpol.polishforforeigners;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.blogspot.dammarpol.polishforforeigners.LearnGreetings.*;
public class LearnGreetingsActivity extends Activity{
TextView englishExpression;
TextView polishExpression;
ProgressBar progressBar;
Button nextButton;
private List<Integer> drawIndex(VocabularyLists lists, List<Integer> notAvailableIndexes, Button nextButton, Random rand){
boolean found = false;
int n;
do{
n = rand.nextInt(lists.englishWords.length);
if(notAvailableIndexes.contains(n)){
found = false;
}
else{
englishExpression.setText(lists.englishWords[n]);
polishExpression.setText(lists.polishWords[n]);
notAvailableIndexes.add(n);
found = true;
}
}while(found);
return notAvailableIndexes;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.learn_greetings);
final VocabularyLists lists = new VocabularyLists();
final List<Integer> notAvailableIndexes = new ArrayList<Integer>();
nextButton = (Button) findViewById(R.id.nextButton);
final Random rand = new Random();
englishExpression = (TextView) findViewById(R.id.learnGreetingsEnglish1);
polishExpression = (TextView) findViewById(R.id.learnGreetingsPolish1);
nextButton.setOnTouchListener(new OnTouchListener() {
@SuppressLint("ClickableViewAccessibility") @Override
public boolean onTouch(View arg0, MotionEvent arg1) {
drawIndex(lists, notAvailableIndexes, nextButton, rand);
return true;
}
});
}
}
VocabularyLists.java
package com.blogspot.dammarpol.polishforforeigners.LearnGreetings;
import java.util.ArrayList;
import java.util.List;
public class VocabularyLists{
public String[] englishWords = {"Good morning", "Good afternoon", "Good evening", "Good night", "Goodbye", "Hello", "Nice to meet you"};
public String[] polishWords = {"Dzień dobry", "Dzień dobry", "Dobry wieczór", "Dobranoc", "Do widzenia", "Cześć", "Miło cię poznać"};
}