1

我正在 Android 中编写一个棋盘游戏,其中 UI 包含分数的 textViews(CPUScore 和 PlayerScore)。我遇到的问题是,当调用 onCreate 时,UI 不会从其初始值更新分数。我看过类似的问题,最建议的解决方案是使用 AsyncTask 在后台更新 UI 线程。但是,我没有找到明确处理如何在 AsyncTask 中使用 textViews 的解决方案。

这是我的尝试:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
//....
setContentView(R.layout.main_layout);

//.....
//------------ textViews declared here don't refresh -------------------
TextView playerScoreForm = (TextView) findViewById(R.id.PlayerTotalScore);
        playerScoreForm.setText(Integer.toString(PlayerTotal));
        playerScoreForm.invalidate();

        TextView CPUScoreForm = (TextView) findViewById(R.id.CPUTotalScore);
        CPUScoreForm.setText(Integer.toString(CPUTotal));
        CPUScoreForm.invalidate();
//--------------------------------------------------------------------------    
//AsyncTask method:    
        new updatePlayerScore().execute(PlayerTotal);
        new updateCPUScore().execute(CPUScoreForm);
    }

AsyncTask 子类:

private class updatePlayerScore extends AsyncTask<TextView, Void, Void> {

        @Override
            protected TextView doInBackground(TextView... params) {


                    // what to put here??



            }
             return playerScoreForm;
         }

            @Override
         protected void onProgressUpdate(Integer... values) {
             //??
         }

         protected void onPostExecute(Integer result) {
            playerScoreForm.setText(Integer.toString(result));
         }

     }


    private class UpdateCPUScore extends AsyncTask<TextView, Integer, Integer> {
        // same syntax as updatePlayerScore

     }

问题: 如何将我在 onCreate 方法中声明的 textViews 转移到 AsyncTask 方法中?我难住了。我对Android开发相当陌生。

4

2 回答 2

2

a)我很确定您在设置 TextViews 后不需要使它们无效;Android 应该自动执行此操作。

b)理论上,您可以将TextView引用设置为成员变量,然后引用它们onPostExecute而不是将它们传递给doInBackground. doInBackground反过来将采用任何数据位使您能够计算新分数。你要做的doInBackground是任何会导致计算新分数的动作。from 的返回值doInBackground被传递到onPostExecute. 然后,您将使用onPostExecute. 那有意义吗?您实际上还没有在此处发布任何可以更新这些分值的代码。

请参阅此处以获取快速示例。

private TextView myScoreView;  //initialized in onCreate as you do above.


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    //....
    setContentView(R.layout.main_layout);

    //.....

    myScoreView = (TextView) findViewById(R.id.PlayerTotalScore);
    myScoreView.setText(Integer.toString(PlayerTotal));

    new updatePlayerScore().execute(1,2); //parameters for calculation
}

private class updatePlayerScore extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected TextView doInBackground(Integer... params) {

                int score = params[0] + 2 * params[1];
                return score;
        }


        @Override
        protected void onProgressUpdate(Integer... values) {
            //if you want to provide some indication in the UI that calculation 
            //is happening, like moving a progress bar, that's what you'd do here.
        }

        @Override
        protected void onPostExecute(Integer scoreCalculationResult) {
           myScoreView.setText(Integer.toString(scoreCalculationResult));
        }

 }

编辑:如果您不想在 doInBackgroundThread 中执行计算逻辑,您可能只想使用:

runOnUiThread(new Runnable(){
     @Override
     public void run(){
         myScoreView.setText(PlayerScoreValue);
     }
});

或者:

 myScoreView.post(new Runnable(){
     @Override
     public void run(){
         myScoreView.setText(PlayerScoreValue);
     }
});
于 2012-04-02T22:55:12.447 回答
0

TextView您可以在 的构造函数中传递并从方法AsyncTask 更新它onPostExecute

private class updatePlayerScore extends AsyncTask<Void, Void, Integer> {
private TextView view;
public updatePlayerScore(TextView textView){
this.view = textView;
}
        @Override
            protected Integer doInBackground(Void... params) {
            int score = 0; 

                    //do you calculation the 

             return score;
         }
         protected void onPostExecute(Integer result) {
           view.setText(Integer.toString(result));
         }

     }

注意:如果您的 Activity 配置因任何原因发生更改,即用户旋转设备并且您AsyncTask尚未完成任务,您的更新TextView将不会更新,因此您应该保留 AsyncTask 的实例并更新 TextView

于 2012-04-02T23:00:51.497 回答