0

私のアプリでは、ネットワーク操作を実行する必要があるため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が、いくつかのエラーが発生しました。

これは正しいですか?

4

2 に答える 2

3

だから私の質問は、AsyncTask クラスが "doInBackground" 操作を完了するのを待つために、メイン スレッドで何ができるかということです。

こんなことしないで。のようなものを使用する完全なポイントは、メインアプリケーションスレッドをブロックAsyncTaskないことです。

の「取得した情報をテキスト ビューに入力」してonPostExecute()くださいAsyncTask

于 2013-05-23T20:44:22.233 に答える
1

textView に設定したいデータが何であれ、postExecute メソッドで簡単に実行できます。doInbackground からデータを共有できます。最後の void を文字列として配置し、文字列を渡すだけです。

于 2013-05-23T20:48:53.153 に答える