0

我的应用程序在主要活动上有一个微调器,内容是从 sqlite 加载的。我的应用程序确实为用户提供了一个下载选项,可以从服务器下载最新数据。而异步下载任务在另一个类。

所以我的问题是,一旦下载过程完成,我如何刷新主的活动微调器?

4

1 回答 1

1

像这样试试。

1)创建回调接口

 public interface DownloadCompleted {
        // Define your Data type to which you want to store data after AsycTask completion
        public void onTaskComplete(String result);
    }

2) 异步任务

 public class UploadAsyncTask extends
            AsyncTask<String, Void, String> {
        private ProgressDialog Dialog;
        String response = "";
        Activity _context;
        private DownloadCompleted callback;

        public UploadAsyncTask (Activity _context) {
            this._context = _context;
            this.callback = (DownloadCompleted ) _context;
        }

        @Override
        protected void onPreExecute() {
            Dialog = new ProgressDialog(_context);
            Dialog.setMessage("Loading...");
            Dialog.show();

        }

        @Override
        protected String doInBackground(String... urls) {
            try {
                String url= urls[0];
                //Do your operation
            } catch (Exception e) {
            }
            return response ;

        }

        @Override
        protected void onPostExecute(String result) {
            Dialog.dismiss();
            callback.onTaskComplete(result);

        }

    }

3)在你的调用Activity中实现接口

public class YourActivity extends Activity implements DownloadCompleted {   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new UploadAsyncTask (this).execute(YOUR URL String);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.rss_reader, menu);
        return true;
    }

    @Override
    public void onTaskComplete(String result) {
        // Refresh your view here
                // use result data as after completion AsycTask
    }

}

如需更多帮助和源代码,请访问以下链接。

http://amitandroid.blogspot.in/2013/02/android-rssfeed-with-async-task-example.html

于 2013-03-18T09:03:52.190 回答