Skip to content

Instantly share code, notes, and snippets.

@yuanliwei
Created December 30, 2015 02:52
Show Gist options
  • Save yuanliwei/4eab5961997196fd810c to your computer and use it in GitHub Desktop.
Save yuanliwei/4eab5961997196fd810c to your computer and use it in GitHub Desktop.
Android AsyncTask Example

Android AsyncTask Example

  1. new GetDataTask().execute();

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

	@Override
	protected String[] doInBackground(Void... params) {
		// Simulates a background job.
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
		}
		return mChildStrings;
	}

	@Override
	protected void onPostExecute(String[] result) {
		Map<String, String> newMap = new HashMap<String, String>();
		newMap.put(KEY, "Added after refresh...");
		groupData.add(newMap);

		List<Map<String, String>> childList = new ArrayList<Map<String, String>>();
		for (String string : mChildStrings) {
			Map<String, String> childMap = new HashMap<String, String>();
			childMap.put(KEY, string);
			childList.add(childMap);
		}
		childData.add(childList);

		mAdapter.notifyDataSetChanged();

		// Call onRefreshComplete when the list has been refreshed.
		mPullRefreshListView.onRefreshComplete();

		super.onPostExecute(result);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment