Android: How to update an UI from AsyncTask if AsyncTask is in a separate class?

后端 未结 7 2132
半阙折子戏
半阙折子戏 2020-12-02 12:33

I hate inner class.

I\'ve a main activity who launches a \'short-life\' AsyncTask.

AsyncTask is in a separate file, is not an inner class of

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 12:50

    The Question has already been answered, still im posting how it should be done i guess..

    Mainactivity class

      public class MainActivity extends Activity implements OnClickListener
        {
    
            TextView Ctemp;
    
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Ctemp = (TextView) findViewById(R.id.Ctemp);
                doConv = (Button) findViewById(R.id.doConv);
                doConv.setOnClickListener(this);
            }
    
            @Override
            public void onClick(View arg0) // The conversion to do
            {
                new asyncConvert(this).execute();
            }
        }
    

    now in the async class

    public class asyncConvert extends AsyncTask
    {
        SoapPrimitive response = null;
        Context context;
    
        public asyncConvert(Context callerclass)
        {
            contextGUI = callerclass;
        }
    .
    .
    .
    .
    protected void onPostExecute(String result)
        {
            ((MainActivity) contextGUI).Ctemp.setText(result); // changing TextView
        }
    }
    

提交回复
热议问题