如何使用从一个到下一个的数据更新5个文本视图

时间:2014-03-19 13:21:11

标签: android variables textview

感谢您查看此问题,我希望您能提供帮助

我有5个TextView需要显示最后5个旋转的内容。 但只有lastSpinView.setText(lastSpin);lastSpin2View.setText(lastSpin2);正常工作。 setLast4()似乎让lastSpin3View.setText(lastSpin3); lastSpin4View.setText(lastSpin4); lastSpin5View.setText(lastSpin5);重复了lastSpin2View.setText(lastSpin2)的值,我确定问题出在setLast4()但是想到如何修复它< / p>

public void checkResult(){
    coinResult1();
    coinResult2();
    coinResult = coinresult1 + coinresult2;
    if (coinResult == playerSelection){
        playerWins();
        buttonsReset();
        setLastSpin();
    }
    else {
        playerLost();
        buttonsReset();
        setLastSpin();
    }
   }

   public void playerWins(){
        TextView winner = (TextView) findViewById(R.id.winlose);
        TextView cashView = (TextView) findViewById(R.id.playerCash);
        playerCash = playerCash + (playerBet * playerBonus);
        playerWinLoss = playerBet * playerBonus;
        winner.setText("You Won " + "$" + String.valueOf(playerWinLoss));
        cashView.setText("$" + String.valueOf(playerCash));
   }

   public void playerLost(){
    TextView lost = (TextView) findViewById(R.id.winlose);
        TextView cashView2 = (TextView) findViewById(R.id.playerCash);
        playerCash = playerCash - (playerBet);

        lost.setText("You Lost " + "$" + String.valueOf(playerBet));
        cashView2.setText("$" + String.valueOf(playerCash));
   }

    public void setLastSpin(){
        if (coinResult == 0){
            lastSpin = "HH";

        }
        else if (coinResult == 1){
            lastSpin = "HT";
        }
        else if (coinResult == 2){
            lastSpin = "TT";
        }

        showLastSpin();
        setLast4();
    }

    public void showLastSpin(){
        TextView lastSpinView = (TextView) findViewById(R.id.textView2); 
        TextView lastSpin2View = (TextView) findViewById(R.id.textView3); 
        TextView lastSpin3View = (TextView) findViewById(R.id.textView4); 
        TextView lastSpin4View = (TextView) findViewById(R.id.textView5); 
        TextView lastSpin5View = (TextView) findViewById(R.id.textView6); 


        lastSpinView.setText(lastSpin);
        lastSpin2View.setText(lastSpin2);
        lastSpin3View.setText(lastSpin3);
        lastSpin4View.setText(lastSpin4);
        lastSpin5View.setText(lastSpin5);
    }

    public void setLast4(){
        lastSpin2 = lastSpin;
        lastSpin3 = lastSpin2;
        lastSpin4 = lastSpin3;
        lastSpin5 = lastSpin4;
    }

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我认为你只是颠倒了两行。你想要做的是:

  1. 在变量中获取新值
  2. 将不同的值向下移动一行(在lastSpin(s)中)
  3. 更新视图

    public void setLastSpin(){
        String newValue = "";
        if (coinResult == 0)
            newValue = "HH";
        else if (coinResult == 1)
            newValue = "HT";
        else if (coinResult == 2)
            newValue = "TT";
    
        setLast4();
        lastSpin = newValue;
        showLastSpin();
    }
    
    public void setLast4(){
         lastSpin2 = lastSpin;
    }
    
    public void showLastSpin(){
         TextView lastSpinView = (TextView) findViewById(R.id.textView2); 
         TextView lastSpin2View = (TextView) findViewById(R.id.textView3); 
    
         lastSpinView.setText(lastSpin);
         lastSpin2View.setText(lastSpin2);
    }
    
  4. 您所做的问题是您在根据它更改视图后覆盖lastSpin值并更改本地值

    还有这个

      public void setLast4(){
            lastSpin2 = lastSpin;
            lastSpin3 = lastSpin2;
            lastSpin4 = lastSpin3;
            lastSpin5 = lastSpin4;
        }
    

    你必须反过来做,否则你每次都会失去价值。你将lastSpin放在LastSpin2中,然后将lastSpin2放到lastSpin3中 - &gt;就像你将lastSpin直接放到lastSpin3等。

    所以它应该像

      public void setLast4(){
            lastSpin5 = lastSpin4;
            lastSpin4 = lastSpin3;
            lastSpin3 = lastSpin2;
            lastSpin2 = lastSpin;
        }
    

    所有这一切都考虑到lastSpin是包含最后得分的TV1。

    希望它能解决你的问题;)