gpt4 book ai didi

android - AsyncTask - 执行后,如何更新 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:22 25 4
gpt4 key购买 nike

在 Activity 的 onCreate() 事件中,我启动了一个 AsyncTask 以从数据库中检索产品数据。成功完成后,如何更新显示?

元代码:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.venueviewbasic);
(..)
new GetProductDetails().execute();

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

protected String doInBackground(String... params) {

// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", vid));
(.. retrieve and parse data and set new textview contents ..)

但是 TextView 等不会更新。

最佳答案

如果你想在完成进程后从异步更新 View 你可以使用

protected void onPostExecute(String result)
{
textView.setText(result);
}

但是如果你想在运行后台进程时更新数据,那么使用。例如……

protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));<------
}
return totalSize;
}

protected void onProgressUpdate(Integer... progress) { <-------
setProgressPercent(progress[0]);
}

有关更多详细信息,请参阅 this link希望这对您有所帮助...!

关于android - AsyncTask - 执行后,如何更新 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com