Android Activity 太忙无法设置 TextView 文本?

发布于 2024-11-15 02:42:37 字数 2711 浏览 3 评论 0原文

我有一个函数,用 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();
    }

我尝试做的是在程序开始之前在我的活动上设置文本视图的文本(在第一个“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;
}

i have a function that fill up my SQLite DB with entries of a http query:

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();
    }

What i try to do is to set the text of an textview on my activity before the procedure starts (in front of the first "try{.." in my code i postet).
but the text won't change, because my activity is too busy to get the data ( i think.I have no other explanation..)

any suggestions?

thanks,
prexx

UPDATE
'' Get data from 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();
    }

My Async Task:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在风中等你 2024-11-22 02:42:37

将代码的繁忙部分放入单独的线程中。

查看 AsyncTask 实用程序

调用 AsyncTask.execute()< /code> 就在 textview.setText("foo") 之后,你会没事的:)

问候

代码示例更新:

 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 线程中进行,并且可以在 onPostExecute 方法中完成,该方法将在同一个 UI 线程中执行

Put the busy part of your code into a separated Thread.

Look at the AsyncTask utility

Call AsyncTask.execute() just after textview.setText("foo") and u will be fine :)

Regards

UPDATE with code sample:

 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!");
}

}

The key is to put all busy code into the doInBackGround method which will runs in a separate thread. All UI modification must be in the same UI thread and this could be done into the onPostExecute method which will be executed in the same UI thread

淡看悲欢离合 2024-11-22 02:42:37

您可以尝试在 TextView 上调用 invalidate() 。但使用异步任务是重数据加载方法的最佳实践。这不会中断用户操作 UI 控件。

You could try to call invalidate() on TextView. But using asynchronous tasks is a best practice for heavy data load methods. That will not interrupt user in operation UI controls.

惟欲睡 2024-11-22 02:42:37

它与“太忙”无关,而是与只有在方法返回时才会设置文本这一事实有关。通过你的网络,这将会被延迟。

顺便提一句。在 UI 线程上的 Honeycomb 网络上将抛出异常并终止您的应用程序。

It has nothing to do with "too busy" but with the fact that the text will only be set when the method returns. And with your networking this will be delayed.

Btw. on Honeycomb network on UI thread will throw an Exception and kill your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文