Created
February 25, 2016 02:04
-
-
Save npike/055c99b6053a925a4c18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BuildingModel { | |
private String mName; | |
private String mAddress; | |
public BuildingModel(String name, String address) { | |
mName = name; | |
mAddress = address; | |
} | |
public String getName() { | |
return mName; | |
} | |
public String getAddress() { | |
return mAddress; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BuildingLoader extends Loader<ArrayList<BuildingModel>> { | |
private final String[] mBuildingNames; | |
private ArrayList<BuildingModel> mBuildingArrayList; | |
public BuildingLoader(Context context, String[] buildingNames) { | |
super(context); | |
mBuildingNames = buildingNames; | |
} | |
@Override | |
protected void onStartLoading() { | |
if (mBuildingArrayList != null) { | |
deliverResult(mBuildingArrayList); | |
} else { | |
forceLoad(); | |
} | |
} | |
@Override | |
protected void onForceLoad() { | |
if (mBuildingArrayList != null) { | |
deliverResult(mBuildingArrayList); | |
} | |
loadInBackground(); | |
} | |
@Override | |
protected void onReset() { | |
mBuildingArrayList = null; | |
} | |
private void loadInBackground() { | |
for (int x = 0; x < mBuildingNames.length; x++) { | |
onGetDataForBuilding(mBuildingNames[x]); | |
} | |
deliverResult(mBuildingArrayList); | |
} | |
@VisibleForTesting | |
protected void onGetDataForBuilding(String buildingName) { | |
// Blocking call to a thirdparty REST API that accepts a building name | |
// and returns the building address. | |
BuildingModel resultBuilding = null; | |
onSuccess(resultBuilding); | |
} | |
public void onSuccess(final BuildingModel buildingModel) { | |
if (mBuildingArrayList == null) { | |
mBuildingArrayList = new ArrayList<>(); | |
} | |
mBuildingArrayList.add(buildingModel); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment