我已经正确初始化了所有变量,但它一直返回-1。
private void RandomCard(int spreadname, int spreadposition) {
try {
SecureRandom rand = SecureRandom.getInstanceStrong();
Collections.shuffle(list_unusedCards, rand);
Random ranNum = new Random();
int numUnusedCards = list_unusedCards.size();
int pickedCard = ranNum.nextInt(numUnusedCards);
str_cardName = list_unusedCards.get(pickedCard).GetName();
str_cardDesc = list_unusedCards.get(pickedCard).GetDescription();
str_imageName = list_unusedCards.get(pickedCard).GetImageName();
String str_spreadPos = Integer.toString(spreadposition);
cl_chosenCards = new CardList(str_cardName, str_cardDesc, str_deckChosen, str_imageName, str_spreadPos);
list_chosenCards.add(cl_chosenCards);
String btnPos = list_chosenCards.get(0).GetButtonPosition(); //GetButtonPosition returns value of str_spreadPos
int int_index = list_chosenCards.indexOf(str_spreadPos);
tv_test0.setText(str_cardName);
tv_test1.setText(str_cardDesc);
//tv_test2.setText("spread pos: " + str_spreadPos); //WORKS
//tv_test2.setText("button pos: " + btnPos); //WORKS
//tv_test2.setText("index: " + int_index); //DOESN'T WORK, RETURNS -1
对于我注释掉的最后 3 行代码,我一直在测试这些变量是否被正确分配以及它们是否存在于列表中。它们不为空,它们返回正确的值。但由于某种原因,第三个一直返回-1。