ArrayList返回错误的结果

时间:2019-01-17 23:57:26

标签: java android arraylist

我正在使用ArrayList将播放器名称存储在我的adnroid应用程序中。我使用列表中的索引来命名这些名称,并使用它们来更新文本视图。但是,即使有时索引正确,有时也可能会拉出错误的列表条目,但这并不总是可行。

我不知道要怎么解决它,因为我不知道它为什么会发生。

PlayerNames.java

   playerArray = new ArrayList<>();

    saveName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = enterName.getText().toString();
            playerArray.add(name);
            setPlayerNames();

        }
    });

QuizQuestions.java

    private ArrayList playerList = null;
private int z;
public class getQuestion extends AsyncTask<String, Void, String> { 
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);


        try{
            Intent intent = getIntent();
            Bundle impExtras = intent.getExtras();
            z = (int) impExtras.get("NEXT");
        } catch(Exception e){
            z = 0;
        }


        playerList = PlayerNames.getPlayerArray();

        if (z < (playerList.size() - 1) && playerList.size() != 0){
            Log.i("index z: ", String.valueOf(z));
            Log.i("list entry at index z: ", playerList.get(z).toString());
            currPlayer.setText(playerList.get(z).toString());
            Log.i("playerName: ", currPlayer.getText().toString());
            z++;


        } else if (z==(playerList.size() - 1) && playerList.size() != 0){
            Log.i("index z: ", String.valueOf(z));
            Log.i("list entry at index z: ", playerList.get(z).toString());
            currPlayer.setText(playerList.get(z).toString());
            Log.i("playerName: ", currPlayer.getText().toString());
            z=0;

        } else{
            currPlayer.setText("Anon");
        }


        playerNow = currPlayer.toString();

        Bundle extras = new Bundle();

        extras.putInt("NEXT", z);



    }

CorrectAnswer.java

     private String currPlayer;
private ArrayList playerList = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    playerList = PlayerNames.getPlayerArray();
    Collections.shuffle(playerList,new Random(System.nanoTime()));

    Intent intent = getIntent();
    final Bundle extras = intent.getExtras();
    currPlayer = (String) extras.get("PLAYER");

    if (playerList.size() > 0) {
        player = playerList.get(0).toString();
        if (player.equals(currPlayer)) {
            player = playerList.get(1).toString();
        }
    } else {
        player = "Anonymous";
    }


    giveDrinks.setText("Give " + player + " " + (i) + " drinks");
    giveDrinks.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent nextQuest = new Intent(CorrectAnswer.this, QuizQuestions.class);
            nextQuest.putExtras(extras);
            startActivity(nextQuest);
            finish();
        }
    });

    challenge.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent newChallenge = new Intent(CorrectAnswer.this, Challenge.class);
            newChallenge.putExtras(extras);
            startActivity(newChallenge);
            finish();
        }
    });



}

我的Logcat

I/index z:: 0    
I/list entry at index z:: player name at index 0    
I/playerName:: player name at index 0    
I/index z:: 1    
I/list entry at index z:: player name at index 1    
I/playerName:: player name at index 1    
I/index z:: 2    
I/list entry at index z:: player name at index 2    
I/playerName:: player name at index 2    
I/index z:: 0    
I/list entry at index z:: player name at index 2    
I/playerName:: player name at index 2     
I/index z:: 1    
I/list entry at index z:: player name at index 0    
I/playerName:: player name at index 0    

我真的不明白。

2 个答案:

答案 0 :(得分:1)

ArrayList必须已经在代码中的其他地方进行了突变,使得这些项在被onPostExecute提取之前处于改变位置。

答案 1 :(得分:1)

无法修改的列表

如果您希望List永远不要更改其元素,请使其不可修改。然后,如果您在执行过程中的某处尝试添加,删除或替换元素的代码,则会引发异常。

在Java的最新版本中,使用新的List.of方法。

在较旧的Java中,使用Collections实用程序类。将您的列表传递到static类方法unmodifiableList。您将获得一个依赖于原始列表的List实现。因此,如果您希望单独修改原始文件,并且不希望不可修改列表的用户看到这些更改,请将原始文件的副本提供给unmodifiable