问题描述
我是充分利用我的AsyncTask?有人可以纠正我的codeS,如果它是错误的。我只是想确保我的AsyncTask全面工作,所以我不会在未来获得任何麻烦。我想使用的AsyncTask我所有的课程。它是一个好的做法呢?
公共类SingleMenuItemActivity延伸活动{
// XML节点键
静态最后弦乐KEY_TITLE =称号;
静态最后弦乐KEY_ARTIST =艺术家;
静态最后弦乐KEY_THUMB_URL =thumb_url;
私人ProgressDialog pDialog;
字符串称号;
字符串的艺术家;
字符串IMAGE_URL;
ImageView的观点;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.single_list_item);
新loadSingleView()执行()。
鉴于=(ImageView的)findViewById(R.id.single_image);
}
公共类loadSingleView扩展的AsyncTask<字符串,字符串,字符串> {
@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(
SingleMenuItemActivity.this);
pDialog.setMessage(连接到服务器...);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@覆盖
保护字符串doInBackground(字符串参数... args){
//从后台线程更新UI
意图= getIntent();
IMAGE_URL = in.getStringExtra(KEY_THUMB_URL);
标题= in.getStringExtra(KEY_TITLE);
艺术家= in.getStringExtra(KEY_ARTIST);
返回null;
}
@覆盖
保护无效onPostExecute(字符串参数){
让所有的产品之后//关闭该对话框
ImageLoader的imgLoader =新ImageLoader的(getApplicationContext());
imgLoader.DisplayImage(图片网址,查看);
TextView的lblName =(TextView中)findViewById(R.id.name_title);
TextView的lblCost =(TextView中)findViewById(R.id.name_artist);
lblName.setText(职称);
lblCost.setText(艺术家);
pDialog.dismiss();
}
}
您需要更新的方法,在UI onPostExecute()
。我喜欢将我的任务分解成他们自己的文件。这样,我们的关注点分离,这使得它更容易理解的code。我们可以用一个接口来定义一个回调方法做到这一点
公共类LoadSingleView扩展的AsyncTask<字符串,字符串,字符串> {
公共接口LoadSingleViewHandler {
无效onSingleViewLoad(字符串结果);
}
私人LoadSingleViewHandler处理程序;
公共LoadSingleView(LoadSingleViewHandler处理){
this.handler =处理程序;
}
@覆盖
保护字符串doInBackground(字符串参数... args){
//做手术这里并返回结果
//操作通常是一些网络请求
//或者一些需要大量的时间
}
@覆盖
保护无效onPostExecute(字符串结果){
handler.onSingleViewLoad(结果);
}
}
现在刚刚开始从活动的任务,并将活动的实施 LoadSingleViewHandler
接口。
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();
}
}
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);
}
}
Now just start the task from the activity and have the activity implement LoadSingleViewHandler
interface.
这篇关于安卓的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!