Domanda

ho una funzione che riempie il mio DB SQLite con le voci di una query http:

try {
        stringEntity = new StringEntity(SQL);
        httpPost.setEntity(stringEntity);
        httpResponse = httpClient.execute(httpPost);
        httpEntity = httpResponse.getEntity();
        bos = new ByteArrayOutputStream();
        httpEntity.writeTo(bos);

        data = bos.toString();

        reader = new BufferedReader(
                  new StringReader(data));

        try {
            //SAVE DATA IN MY DB || WORKS
        } catch(IOException e) {
          e.printStackTrace();
        }

    } catch (IOException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }

Quello che cerco di fare è impostare il testo di un textview sulla mia attività prima che inizi la procedura (davanti al primo "try {.." nel mio codice i postet). ma il testo non cambierà, perché la mia attività è troppo impegnata per ottenere i dati (credo, non ho altra spiegazione ..)

qualche suggerimento?

grazie, prexx

AGGIORNAMENTO "Ottieni dati da AsyncTask"

 txtAction.setText("Loading...");

    AsyncTask<String, String, String> test = new cAsyncTask();

    try {
        data = test.execute(URL).get();

        reader = new BufferedReader(
                  new StringReader(data));

        while ((line = reader.readLine()) != null) {
            //SAVE DATA IN DB || WORKS
            }
        }

    } catch(IOException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

La mia attività asincrona:

class cAsyncTask extends AsyncTask<String, String, String> {

protected String doInBackground(String... urls) {
    int count = urls.length;
    String data = "";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost;
    StringEntity stringEntity;
    HttpResponse httpResponse;
    HttpEntity httpEntity;
    ByteArrayOutputStream bos;
    String line;
    BufferedReader reader;
    for (int i = 0; i < count; i++) {
        httpPost = new HttpPost(urls[i].toString());
        try {
            stringEntity = new StringEntity(SQL);
            httpPost.setEntity(stringEntity);
            httpResponse = httpClient.execute(httpPost);
            httpEntity = httpResponse.getEntity();
            bos = new ByteArrayOutputStream();
            httpEntity.writeTo(bos);

            data = bos.toString();

        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
    }
    return data;
}

protected void onProgressUpdate(String... progress) {

}

protected void onPostExecute(String result) {
    String test = result;
}
È stato utile?

Soluzione

Metti la parte occupata del tuo codice in un thread separato.

Guarda l'utilità AsyncTask

Chiama AsyncTask.execute() subito dopo textview.setText("foo") e starai bene :)

Saluti

AGGIORNAMENTO con esempio di codice:

 txtAction.setText("Loading...");
 AsyncTask<String, String, String> test = new cAsyncTask();
 test.execute("http://...");

class cAsyncTask extends AsyncTask<String, String, String> {

protected String doInBackground(String... urls) {
    int count = urls.length;
    String data = "";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost;
    StringEntity stringEntity;
    HttpResponse httpResponse;
    HttpEntity httpEntity;
    ByteArrayOutputStream bos;
    String line;
    BufferedReader reader;
    for (int i = 0; i < count; i++) {
        httpPost = new HttpPost(urls[i].toString());
        try {
            stringEntity = new StringEntity(SQL);
            httpPost.setEntity(stringEntity);
            httpResponse = httpClient.execute(httpPost);
            httpEntity = httpResponse.getEntity();
            bos = new ByteArrayOutputStream();
            httpEntity.writeTo(bos);

            data = bos.toString();

        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
    }
     reader = new BufferedReader(
                  new StringReader(data));
       String line = "";
        while ((line = reader.readLine()) != null) {
            //SAVE DATA IN DB || WORKS
     }
    return data;
}


protected void onPostExecute(String result) {
    String test = result;
   textView.setText("Done!");
}

}

La chiave è inserire tutto il codice occupato nel metodo doInBackGround che verrà eseguito in un thread separato.Tutte le modifiche dell'interfaccia utente devono essere nello stesso thread dell'interfaccia utente e questo potrebbe essere fatto nel metodo onPostExecute che verrà eseguito nello stesso thread dell'interfaccia utente

Altri suggerimenti

Potresti provare a chiamare invalidate () su TextView.Tuttavia, l'utilizzo di attività asincrone è una procedura consigliata per i metodi di caricamento di dati pesanti.Ciò non interromperà l'utente durante i controlli dell'interfaccia utente.

Non ha nulla a che fare con "troppo occupato" ma con il fatto che il testo verrà impostato solo quando il metodo ritorna.E con la tua rete questo sarà ritardato.

Btw.sulla rete Honeycomb sul thread dell'interfaccia utente genererà un'eccezione e terminerà la tua app.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top