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 ..