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接收的参数的类型 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值