私のアプリでは、ネットワーク操作を実行する必要があるためAsyncTask
、バックグラウンドで実行するクラスを作成しましたが、取得した情報をテキストビューに入力する必要があります。
AsyncTask
だから私の質問は、クラスがdoInBackground
操作を完了するのを待つためにメインスレッドで何ができるかです
これは私の AsyncTask クラスです:
public class ObtenerDatos extends AsyncTask<Void, Void, Void> {
private PuertosSaxParser saxparser;
private ArrayList<clsPuertos> Puertos;
/** Return the Puertos info to the main class */
public ArrayList<clsPuertos> getPuertos() {
return Puertos;
}
/** Get the XML info and do the parse */
@Override
protected Void doInBackground(Void... params) {
try {
saxparser = new PuertosSaxParser("http://www.androide.comyr.com/valores.xml");
Puertos = saxparser.parseXML();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
そして、これは私がメインスレッドから呼び出す方法です:
try {
int_datos.execute();
//HERE I NEED TO WAIT FOR THE AsyncTask TO COMPLETE THE doInBackground OPERATIONS
Puertos = int_datos.getPuertos();
//THEN I FILL THE TEXTVIEWs WITH THE INFO OF Puertos
} catch (Exception e) {
e.printStackTrace();
}
ご協力いただきありがとうございます!
編集:
メインクラス:
ObtenerDatos int_datos = new ObtenerDatos(this);
int_datos.execute();
ObtenerDatos (AsyncTask クラス):
public class ObtenerDatos extends AsyncTask<Void, Void, Void> {
private PuertosSaxParser saxparser;
private ArrayList<clsPuertos> Puertos;
private Context contexto;
final TextView textSanferPleamarHora = new TextView(contexto);
public ObtenerDatos(Context contexto){
this.contexto = contexto;
}
メインからAsyncTask
クラスにコンテキストを渡して設定しようとしましたTextViews
が、いくつかのエラーが発生しました。
これは正しいですか?