Android活动太忙而无法设置TextView文本?

| 我有一个函数用http查询的条目填充我的SQLite数据库:
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();
    }
我想做的是在过程开始之前在我的活动上设置textview的文本(在我发布的代码中的第一个\“ try {.. \”前面)。 但文字不会更改,因为我的活动太忙而无法获取数据(我想。我没有其他解释。) 有什么建议么? 谢谢, prexx 更新 \'\'从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();
    }
我的异步任务:
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;
}
    
已邀请:
        将代码的繁忙部分放入单独的线程中。 看一下AsyncTask实用程序 在
textview.setText(\"foo\")
之后再致电
AsyncTask.execute()
,您可以:) 问候 使用代码示例进行更新:
 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!\");
}

}
关键是将所有繁忙代码放入
doInBackGround
方法中,该方法将在单独的线程中运行。所有UI修改都必须在同一UI线程中,并且可以在将在同一UI线程中执行的
onPostExecute
方法中完成     
        您可以尝试在TextView上调用invalidate()。但是,对于繁重的数据加载方法,使用异步任务是最佳实践。这不会中断用户在操作UI控件中的操作。     
        它与“太忙”无关,而是与文本仅在方法返回时才设置有关。与您的网络连接将被延迟。 顺便说一句。在UI线程上的Honeycomb网络上,将引发异常并杀死您的应用程序。     

要回复问题请先登录注册