1

I have this code in Main Thread:

// in MyAsyncTask doinBackground and onPostExecute Fill up my secondArray with items

public class MainActivity extends Activity {

    ArrayList<String> secondArray = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView lv= (ListView) findViewById(R.id.listView);
        final ListView lv2= (ListView) findViewById(R.id.listView2);
          AsyncTas o = new AsyncTas();

        Toast.makeText(MainActivity.this, secondArray.get(0), Toast.LENGTH_LONG).show();

    }

    private  class AsyncTas extends AsyncTask  {
ArrayList<String> firstArray = new ArrayList<String>();
        @Override
        protected ArrayList<String> doInBackground(Object[] params) {
            firstArray.add("item1");
            firstArray.add("item2");
            firstArray.add("item3");
            try {
                Thread.sleep(1200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return firstArray;

        }
        @Override
        protected void onPostExecute(ArrayList<String> o) {
            secondArray.clear();
            secondArray.addAll(o);

        }
    }
}

But I have an Error caused by java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

My Question Is How to hang up or let Main thread waits untill Background ends its all processing and thank you ??

I found an answer in Waiting till the async task finish its work but it not work with me ..

Community
  • 1
  • 1
Ahmed
  • 57
  • 9
  • What is the point of having a background thread if you will hold up the main thread till processing in background thread is completed? – Chebyr Jul 02 '16 at 14:01
  • @Chebyr Beacause I am waiting for some connections and getting some peaces of data then It will be processed in another AsyncTask – Ahmed Jul 02 '16 at 14:12

3 Answers3

1

2 points - you are not actually executing your async task - you create it with

AsyncTas o = new AsyncTas();

but you need to then call

o.execute();

The next problem is that the Toast immediately tries to access the data which will be created by the Task. You need to move

Toast.makeText(MainActivity.this, secondArray.get(0), Toast.LENGTH_LONG).show();

...to the onPostExecute() method of the Task:

@Override
        protected void onPostExecute(ArrayList<String> o) {
            secondArray.clear();
            secondArray.addAll(o);
            Toast.makeText(MainActivity.this, secondArray.get(0), Toast.LENGTH_LONG).show();
        }

The means the Toast will only execute once the data is ready and execute in the UI thread.

Ramsay Domloge
  • 695
  • 3
  • 11
  • Thank you Ramasay , but I do not want to execute this part in onPostExecte method I want to execute it in main thread . – Ahmed Jul 02 '16 at 14:03
  • Well, I think you mean you want the Toast to be executed in the UI thread, which is the same thread that creates the Activity and calls onCreate(). Luckily, the onPostExecute() method runs in the UI thread, so mission accomplished! – Ramsay Domloge Jul 02 '16 at 14:12
0

Please execute aysnctask first then in its onPostExecute add all response in array then find this array.get(0). Your array is still empty but you are trying to get value at zeroth position.

new AsyncTas().execute();

then in onPostExecute() -

secondArray.clear();
secondArray.addAll(o);
Toast.makeText(MainActivity.this, secondArray.get(0), Toast.LENGTH_LONG).show();

Hope it will help.

Vikram
  • 768
  • 2
  • 9
  • 23
  • thank you Vikram , my problem is that I have many background threads but I want to execute this code in Main thread how can i do that. – Ahmed Jul 02 '16 at 14:06
  • this will in main thread only. onPostExecute() is called on main thread. – Vikram Jul 02 '16 at 14:18
0
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity {

    private ArrayList<String> secondArray;
    private ListView lv, lv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView);
        lv2 = (ListView) findViewById(R.id.listView2);

        new Task().execute();
        Toast.makeText(MainActivity.this, secondArray.get(0), Toast.LENGTH_LONG).show();

    }

    class Task extends AsyncTask<Void, Void, ArrayList<String>> {

        @Override
        protected ArrayList<String> doInBackground(Void... params) {
            ArrayList<String> firstArray = new ArrayList<String>();
            firstArray.add("item1");
            firstArray.add("item2");
            firstArray.add("item3");
            return firstArray;
        }

        @Override
        protected void onPostExecute(ArrayList<String> strings) {
            super.onPostExecute(strings);
            secondArray = strings;
        }
    }
}