刷新游戏使用的AsyncTask的TextView分数 [英] Refresh Game Score TextView using AsyncTask

查看:187
本文介绍了刷新游戏使用的AsyncTask的TextView分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Android的棋盘游戏,其中包括用户界面为textViews分数(CPUScore和PlayerScore)的。我的问题是,当的onCreate称为UI不更新从初始值的分数。我已经看过类似的问题,大部分建议解决方案是使用AsyncTask的更新在后台UI线程。但是我没有找到一个解决方案,与如何使用AsyncTask的明确textViews处理。

I am writing a board game in Android where the UI consists of textViews for the scores (CPUScore and PlayerScore). The problem I have is that the UI does not update the score from its initial value when onCreate is called. I have looked at similar questions and the solution most suggested is to use AsyncTask to update the UI thread in the background. However I did not find a solution that dealt explicitly with how to use textViews in AsyncTask.

下面是我的尝试:

@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的子类:

The AsyncTask subclasses:

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方法的AsyncTask的方法声明的textViews?我难倒。我是相当新的Andr​​oid开发。

Question: how do I transfer the textViews that I declared in the onCreate method to the AsyncTask method? I am stumped. I am fairly new to Android development.

推荐答案

一)我是pretty确保你不应该需要你设置后,它们将无效TextViews; Android的应该这样做自动的。

a) I'm pretty sure you shouldn't need to invalidate the TextViews after you set them; Android should do that automagically.

b)在理论上你设置你的的TextView 引用视为成员变量,然后在 onPostExecute 引用它们而不是将它们传递到 doInBackground doInBackground 反过来将采取任何的数据位使您能够计算出新的成绩。你会做什么的 doInBackground 是什么动作可能会导致要计算一个新的成绩。从 doInBackground 的返回值被传递到 onPostExecute 。然后,您可以更新 onPostExecute TextView的(现在是一个成员变量)与此数据。那有意义吗?您还没有真正发布任何code在这里,将更新的分值。

b) In theory you'd set your TextView references to be member variables and then reference them in onPostExecute instead of passing them into doInBackground. doInBackground in turn will take whichever bits of data enable you to calculate the new score. What you would do on doInBackground is whatever action would cause a new score to be calculated. The return value from doInBackground gets passed into onPostExecute. You would then update the TextView (now a member variable) with this data in onPostExecute. Does that make sense? You haven't actually posted any code here that would update those score values.

请参阅这里一个简单的例子。

See here for a quick example.

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计算逻辑,你可能只是想用:

If you don't want to do the calculation logic in doInBackgroundThread, you probably just want to use:

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

或者

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

这篇关于刷新游戏使用的AsyncTask的TextView分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆