how to pass in two different data types to AsyncTask, Android

前端 未结 3 1951
醉酒成梦
醉酒成梦 2021-01-04 04:34

i have a method that does an SQLite database update and put this inside of an AsyncTask to make it faster and more reliable.

however there are two pieces of data tha

3条回答
  •  悲&欢浪女
    2021-01-04 05:24

    Just pass the required object in the constructor of your async task and later use this object in doinBackground().You can create constructor and pass the your object in the following way:

    public class MyAsyncTask extends AsyncTask{
    
        PrimaryKeySmallTank tankObj;
    
        public UpdateInfoAsyncTask(PrimaryKeySmallTank obj ){
            tankObj=obj;
        }
    
        @Override
        protected void onPreExecute() {
           // TODO Auto-generated method stub
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            //code and use tankObj
             return null;
        }
    }
    

    In your code pass the required object:

    new myAsyncTask(new PrimaryKeySmallTank (1,1,1,"hi",1).execute();
    

提交回复
热议问题