How to get a string back from AsyncTask?

前端 未结 8 2016
Happy的楠姐
Happy的楠姐 2020-12-03 04:01

I have the following class:

public class getURLData extends AsyncTask{

@Override
protected String doInBackground(String... pa         


        
8条回答
  •  借酒劲吻你
    2020-12-03 04:40

    Add a context parameter to task's constructor which would refer to object where you'd store resulting data.

    class PopulateArray extends AsyncTask>
    {
        Context _context; // context may be what ever activity or object you want to store result in
        PopulateArray(Context context)
        {
            _context = context;
        }
        @Override
        protected ArrayList doInBackground(Integer... integers)
        {
            ArrayList data = new ArrayList();
            //manipulate
            return data;
        }
    
        @Override
        protected void onPostExecute(ArrayList strings)
        {
            _context.setData(strings);
        }
    }
    

提交回复
热议问题