安卓异步任务 [英] Android AsyncTask
问题描述
我是否充分利用了我的 AsyncTask?如果我的代码有误,有人可以更正我的代码.我只是想确保我的 AsyncTask 完全正常工作,这样我将来就不会遇到任何麻烦.我希望为我的所有课程使用 AsyncTask.这是一个好习惯吗?
Am i fully utilising my AsyncTask? Can someone correct my codes if it is wrong. I just want to make sure my AsyncTask fully works so i wont get any trouble in the future. I wish to use AsyncTask for all my classes. Is it a good practice?
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();
}
}
推荐答案
您必须从方法 onPostExecute()
更新 UI.我喜欢将我的任务移动到他们自己的文件中.通过这种方式,我们可以分离关注点,从而更容易理解代码.我们可以通过使用一个接口来定义一个回调方法来做到这一点
You have to update the UI from the method onPostExecute()
. I like to move my task into their own files. This way we have a separation of concerns which makes it more easier to understand the code. We can do this by using a interface to define a callback method
public class LoadSingleView extends AsyncTask<String, String, String> {
public interface LoadSingleViewHandler {
void onSingleViewLoad(String result);
}
private LoadSingleViewHandler handler;
public LoadSingleView(LoadSingleViewHandler handler) {
this.handler = handler;
}
@Override
protected String doInBackground(String... args) {
// Do operation here and return the result
// Operation is usually some network request
// or something that will take alot of time
}
@Override
protected void onPostExecute(String result) {
handler.onSingleViewLoad(result);
}
}
现在只需从 Activity 启动任务并让 Activity 实现 LoadSingleViewHandler
接口.
Now just start the task from the activity and have the activity implement LoadSingleViewHandler
interface.
这篇关于安卓异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!