1

I have a ArrayList<Integer>

and i want to pass it to AsyncTask<ArrayList<Integer>, void, void>. But in doInBackground(ArrayList<Integer>...params) function, i can't receive arrayList, which i passed.

Inside doInBackground i use ArrayList<Integer> arr = params[0] then i log(arr.size()) is 0 My code:

class count  extends AsyncTask<Void, Integer, ArrayList<Integer>>{

    ArrayList<Integer> arr = new ArrayList<Integer>();
    ArrayList<Integer> temp = new ArrayList<Integer>();
    @SuppressWarnings("unchecked")
    @Override
    protected ArrayList<Integer> doInBackground(Void... params) {
        // TODO Auto-generated method stub
        for(int i = 1; i <= 100; i++){
            SystemClock.sleep(200);
            arr.add(i);
            if(i % 10 == 0){
                temp = arr;
                //Log.d("DEBUG", "Length of temp = "+ temp.size());
                arr.clear();
                mean task1 = new mean();
                task1.execute(temp);
            }
            publishProgress(i);
        }
        return arr;
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        tvNum.setText(values[0]+"");
    }

}

class mean extends AsyncTask<ArrayList<Integer>, Integer, ArrayList<Integer>>{

    @Override
    protected ArrayList<Integer> doInBackground(
            ArrayList<Integer>... params) {
        // TODO Auto-generated method stub
        ArrayList<Integer> arrL =new ArrayList<Integer>();
        arrL= params[0];
        Log.d("DEBUG","iNPUT Size = " + arrL.size());
        return null;
    }

}

Please help me, Thanks.

2
  • What are you passing when you call execute for the AsyncTask? Commented Apr 27, 2014 at 11:36
  • i pass MyTask.execute(MyList); Commented Apr 27, 2014 at 11:55

2 Answers 2

1

If you pass the Arraylist in as the only parameter when you're calling execute(), it should be in params[0]. For example,

   YourTask.execute(YourList);

And you would access it inside of the ASyncTask as so:

Arraylist<Integer> myList = params[0]; 
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, i do like you. But, for example i have a arrayList and it has 10 elements. When i pass ourTask.execute(arrayList) and inside of doInBAckground Arraylist<Integer> myList = params[0]. Then i Log(myList.size()), it has 0 element. So sad,:(
Then there's most likely a problem somewhere else in your code. If there was something wrong with how you were sending in or receiving the Arraylist then calling .size() in this case would throw a NullPointerException.
Yes, i understand. Below is my code, can you debug?
Your problem is from clearing your 'arr' variable. I understand what you're trying to do with setting temp to arr and then clearing arr, you are assuming that temp will be the values in arr before being cleared. But when you set temp to arr, temp is actually referencing arr, so anything done to arr is also done to temp. In java, all objects are considered references.
That's right! i edit temp = (ArrayList<Integer>) arr.clone(). It worked! Thanks you very much
1

Easy Example for your understanding. such as

public class MainActivity extends Activity {
    List<CalcPrimesTask> taskList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        taskList = new ArrayList<CalcPrimesTask>();
    }

    public void onClickStart(View view) {
        EditText maximumEditText = (EditText) findViewById(R.id.maximumEditText);
        int maxNum = Integer.parseInt(maximumEditText.getText().toString());
        CalcPrimesTask task = new CalcPrimesTask(this);
        taskList.add(task);
        task.execute(maxNum);
        Toast.makeText(getApplicationContext(), "New run queued.", Toast.LENGTH_SHORT).show();
    }

    public void onStopClick(View view) {
        for (CalcPrimesTask task : taskList) {
            task.cancel(true);
        }
        Toast.makeText(getApplicationContext(), "All runs cancelled.", Toast.LENGTH_SHORT).show();
    }
}

and

public class CalcPrimesTask extends AsyncTask<Integer, String, List<Integer>> {

    Activity activity;

    public CalcPrimesTask(Activity mainActivity) {
        activity = mainActivity;
    }

    @Override
    protected List<Integer> doInBackground(Integer... params) {
        int maxNum = params[0];
        List<Integer> primeList = new ArrayList<Integer>();
        for (int i = 2; i <= maxNum ; i++) {
            int maxCalc = (int)Math.sqrt(i);
            boolean isPrime = true;
            for (int j = 2; j <= maxCalc ; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                primeList.add(i);
                publishProgress("Prime " + i + " found.");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
        return primeList;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        TextView messageView = (TextView) activity.findViewById(R.id.messageText);
        messageView.setText(values[0]);
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(List<Integer> result) {
        TextView messageView = (TextView) activity.findViewById(R.id.messageText);
        messageView.setText("Total of " + result.size() + " primes found.");
        super.onPostExecute(result);
    }
}

If your hand available time then read Android AsyncTask. Best of Luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.