I am trying to figure out a way to get specific values that are needed for the objects displayed in a ListView
. In the example below, I am building a list of users. I use an existing array to supply some of the values but need to dynamically check for/get pictures associated with that user from the server.
private void getUsers(JSONArray userArray){
users = new ArrayList<User>();
int length = userArray.length();
if(DEBUG) Log.d(TAG, userArray.toString());
try{
for(int i = 0; i < length; i++){
User u = new User();
u.setProfileId(userArray.getJSONObject(i).getString("profileid"));
u.setDisplayName(userArray.getJSONObject(i).getString("displayname"));
u.setStatus(userArray.getJSONObject(i).getString("status"));
u.setId(userArray.getJSONObject(i).getString("id"));
if(memCache.contains(u.getDisplayName())){
u.setProfilePicture(memCache.getBitmapFromMemCache(u.getDisplayName()));
} else {
downloadPicture(u.getProfileId(), u.getDisplayName());
}
users.add(u);
}
users.add(createBlankUser());
updateUserListView();
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
}
private void downloadPicture(String profileId, final String displayName){
GetUserProfilePicture getPic = new GetUserProfilePicture(new OnUserPictureDownloaded(){
@Override
public void downloadFinished(Bitmap b) {
if(b != null){
if(DEBUG) Log.d(TAG, "Download finished " + b.toString() + " " + displayName);
memCache.addBitmapToMemoryCache(displayName, b);
}
}
});
getPic.execute(profileId);
}
The AsyncTask
and methods in its class all do what they are supposed to. I get the image returned properly from the server. The issue is that I can't link the result of the AsyncTask
to a particular object before I add it to the list. In case that made no sense, in the past when I was using a private AsyncTask
within this activity I could just run any I/O and object building methods in the background together so that the downloading of the images would correspond to the object being constructed at that time. Now that I have made the AsyncTask
its own class (which is better for the overall structure of the app) I'm having a hard time taking the value returned by the interface and using.
Any suggestions?