Android AsyncTask [英] Android AsyncTask

查看:78
本文介绍了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);
    }
}

现在只需从活动开始任务,并让该活动实现LoadSingleViewHandler接口.

Now just start the task from the activity and have the activity implement LoadSingleViewHandler interface.

这篇关于Android AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆