Refresh Game Score TextView using AsyncTask

倾然丶 夕夏残阳落幕 提交于 2019-12-06 16:53:38

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

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));
        }

 }

Edit: 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);
     }
});

Or:

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

You can pass the TextView in the constructor of the AsyncTask and update it from the onPostExecute method

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));
         }

     }

note: if you Activity configuration change for any reason i.e the user rotate the device and the you AsyncTask hasn't finish it task the update of you TextView will not be updated so you should retain an instance of you AsyncTask and update the the TextView

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!