以下の例のようにカウントダウンラッチを使用しています。ここで、BackgroundDataPopulator は AyncTask を作成し、次に PackagePopulator で displayData を呼び出します。しかし、それは決して起こらず、活動は行き詰まります。ここで何が間違っていますか。
public class PackagePopulator implements ServerDataProcessor {
private CountDownLatch counter=new CountDownLatch(1);
String[] packages;
public void getPagesFromServer() {
new BackgroundDataPopulator(IMgrApplication.getAppContext().getString(R.string.list),new PData(),this).execute();
}
public String[] getPages() {
try {
counter.await();
} catch (InterruptedException e) {
packages=new String[0];
}
return packages;
}
@Override
public void displayData(ServerOutput output) {
Page[] pgArray = new Page[((PageData)output).getPages().size()];
pages=new String[pgArray.length];
pgArray = ((PageData)output).getPages().toArray(pgArray);
for(int i=0;i<pgArray.length;i++){
pages[i]=pgArray[i].getName();
}
counter.countDown();
}
}
public class BackgroundDataPopulator extends AsyncTask<Void, String, ServerOutput> {
// connector=new JSONConnector();
Connector connector;
String curUrl;
ServerOutput curServerOutput;
ServerDataProcessor processor;
public BackgroundDataPopulator(String url, ServerOutput serverOutput,ServerDataProcessor curProcessor) {
//connector = new UnitTestConnector();
connector = new JSONConnector();
curUrl = url;
curServerOutput = serverOutput;
processor=curProcessor;
}
@Override
protected ServerOutput doInBackground(Void... params) {
return connector.getData(URLUtils.getFormattedUrl(curUrl),curServerOutput);
}
@Override
protected void onPostExecute(ServerOutput output) {
processor.displayData(output);
//Toast.makeText(BaseFragmentActivity.this, "Done!",Toast.LENGTH_SHORT).show();
}
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
pagePopulator=new PagePopulator();
pagePopulator.getPagesFromServer();
super.onCreate(savedInstanceState);
}
public ArrayAdapter<CharSequence> getSpinnerDataArray(String item) {
return new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,pagePopulator.getPages());
}