Android Activity 太忙无法设置 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();
}
我尝试做的是在程序开始之前在我的活动上设置文本视图的文本(在第一个“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将代码的繁忙部分放入单独的线程中。
查看 AsyncTask 实用程序
调用
AsyncTask.execute()< /code> 就在
textview.setText("foo")
之后,你会没事的:)问候
代码示例更新:
关键是将所有繁忙的代码放入
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 aftertextview.setText("foo")
and u will be fine :)Regards
UPDATE with code sample:
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 theonPostExecute
method which will be executed in the same UI thread您可以尝试在 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.
它与“太忙”无关,而是与只有在方法返回时才会设置文本这一事实有关。通过你的网络,这将会被延迟。
顺便提一句。在 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.