`
BestUpon
  • 浏览: 284118 次
  • 性别: Icon_minigender_1
  • 来自: 兰州
社区版块
存档分类
最新评论

AsyncTask 的执行顺序很重要

阅读更多

 

@Override
	protected ListAdapter initListViewAdapter() {
		Log.e("--------------", "执行--0");
		new AsynchTaskRequestData().execute();
		Log.e("--------------", "执行--99");
		mAdapter = new DailContactAdapter(this, cInfos, listTag);
		Log.e("--------------", "执行--100");
		return mAdapter;
	}

	private class AsynchTaskRequestData extends AsyncTask<Integer, Void, List<ContactInfo>> {

		@Override
		protected List<ContactInfo> doInBackground(Integer... params) {
			Log.e("--------------", "执行--1");
			try {
				Log.e("--------------", "执行--2");
				Thread.sleep(PtasApplication.THREAD_SLEEP_TIME);
			} catch (InterruptedException e) {
				Log.e(DailContact.this.getClass().getName(), "InterruptedException", e);
			}
			Log.e("--------------", "执行--3");
			return ContactHolder.getContactInfosSort(DailContact.this, getWhere(), getOrder(), listTag);
		}

		@Override
		protected void onPreExecute() {

			Log.e("--------------", "执行--4");
			commonList.addFooterView(loadingView);
		}

		@Override
		protected void onPostExecute(List<ContactInfo> currentCInfos) {

			Log.e("--------------", "执行--5");
			if (cInfos.size() == 0) {
				displayText.setVisibility(View.VISIBLE);
			}
			if (currentCInfos.size() == 0) {
				if (cInfos.size() == 0) {
					displayText.setVisibility(View.VISIBLE);
				}
				commonList.removeFooterView(loadingView);
				return;
			}
			displayText.setVisibility(View.GONE);
			Log.e("--------------", "执行--6");
			cInfos.addAll(currentCInfos);
			mAdapter.notifyDataSetChanged();
			mAdapter.initSections(currentCInfos);
			sections = mAdapter.getSections();
			commonList.removeFooterView(loadingView);
			Log.e("--------------", "执行--7");
		}

	}

 日志记录如下:

12-08 01:00:16.854: E/--------------(571): 执行--0
12-08 01:00:16.873: E/--------------(571): 执行--4
12-08 01:00:16.894: E/--------------(571): 执行--99
12-08 01:00:16.894: E/--------------(571): 执行--100
12-08 01:00:16.923: E/--------------(571): 执行--1
12-08 01:00:16.923: E/--------------(571): 执行--2
12-08 01:00:18.724: I/MapActivity(571): Handling network change notification:CONNECTED
12-08 01:00:18.724: E/MapActivity(571): Couldn't get connection factory client
12-08 01:00:18.924: E/--------------(571): 执行--3
12-08 01:00:27.484: D/dalvikvm(571): GC_FOR_MALLOC freed 9497 objects / 335464 bytes in 85ms
12-08 01:00:34.383: D/dalvikvm(571): GC_FOR_MALLOC freed 12655 objects / 473528 bytes in 75ms
12-08 01:00:38.203: D/dalvikvm(571): GREF has increased to 201
12-08 01:00:40.334: E/--------------(571): 执行--5
12-08 01:00:40.334: E/--------------(571): 执行--6
12-08 01:00:40.684: E/--------------(571): 执行--7
12-08 01:00:42.954: D/dalvikvm(571): GC_FOR_MALLOC freed 12275 objects / 499120 bytes in 71ms

 

结论:如果要在Adapter中处理一些数据,并且将其放在了new Adapter()的构造方法中,这是很危险的,因为在数据还没有处理完成的时候,Adapter 已经被new出来了,在对其进行的后续操作都是都是null。

 

 

结论:执行顺序 onPreExecute-->doInBackground-->onPostExecute 

AsyncTask<Integer, Void, List<ContactInfo>>


第一个是指 doInBackground中接收的参数的类型 
第二个是指onProgressUpdate中接收的参数的类型 
第三个是每日doInBackground返回值的类型以及onPostExecute接收的参数的类型 
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics