gpt4 book ai didi

安卓异步任务

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:19 27 4
gpt4 key购买 nike

我是否充分利用了我的 AsyncTask?如果错误,有人可以更正我的代码。我只想确保我的 AsyncTask 完全正常工作,这样我以后就不会遇到任何麻烦。我希望在所有类(class)中使用 AsyncTask。这是一个好的做法吗?

    public class SingleMenuItemActivity  extends Activity {

// XML node keys
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_THUMB_URL = "thumb_url";
private ProgressDialog pDialog;
String title;
String artist;
String image_url;
ImageView view;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
new loadSingleView().execute();


view = (ImageView) findViewById(R.id.single_image);
}


public class loadSingleView extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(
SingleMenuItemActivity.this);
pDialog.setMessage("Connecting to Server ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// updating UI from Background Thread


Intent in = getIntent();

image_url = in.getStringExtra(KEY_THUMB_URL);
title = in.getStringExtra(KEY_TITLE);
artist = in.getStringExtra(KEY_ARTIST);


return null;

}
@Override
protected void onPostExecute(String args) {
// dismiss the dialog after getting all products

ImageLoader imgLoader = new ImageLoader(getApplicationContext());

imgLoader.DisplayImage(image_url, view);

TextView lblName = (TextView) findViewById(R.id.name_title);
TextView lblCost = (TextView) findViewById(R.id.name_artist);

lblName.setText(title);
lblCost.setText(artist);
pDialog.dismiss();
}
}

最佳答案

您无法从后台线程更新 UI。例如使用doInBackground()从服务器获取数据。所有这些过程将在后台完成。而onPostExecute是在后台进程结束后更新UI

For more info Study here

Example1

Example2

您可以在 Google 中找到更多 :)

关于安卓异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008535/

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