I would like to add elements to the three ArrayLists declared in the code bellow but I seem to be having some issues related to variable scope. Disclaimer: I'm very new to Java and it could just be that I'm doing something very silly. Also note that I'm using the Parse Android API. (I added some comments to the code to better highlight the issue I'm trying to resolve). Thanks!
public class MatchesActivity extends Activity implements OnItemClickListener {
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> descriptions = new ArrayList<String>();
ArrayList<Bitmap> images = new ArrayList<Bitmap>();
String school;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matches_layout);
ParseQuery query = new ParseQuery("Profile");
query.whereEqualTo("userName", ParseUser.getCurrentUser().getUsername().toString());
query.getFirstInBackground(new GetCallback() {
public void done(ParseObject obj, ParseException e) {
if (e == null) {
school = obj.getString("school");
ParseQuery query2 = new ParseQuery("Profile");
query2.whereEqualTo("school", school);
query2.findInBackground(new FindCallback() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
// scoreList.size() == 3 here
for (int i = 0; i < scoreList.size(); i++){
titles.add(scoreList.get(i).getString("fullName"));
descriptions.add(scoreList.get(i).getString("sentence"));
ParseFile profileImg = (ParseFile) scoreList.get(i).get("pic");
try {
profileImg.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0,data.length);
images.add(bMap);
} else {
Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_SHORT).show();
}
// AT THIS POINT THE ARRAYLIST "IMAGES" IS BEING ASSIGNED VALUES
}
});
} catch (NullPointerException npe) {
images.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_prof));
}
}
// HERE, THE SIZE OF TITLES AND DESCRIPTION IS 3, HOWEVER, IMAGES HAS NO ELEMENTS (WHEN I EXPECTED IT TO HAVE 3)
} else {
Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
// ALL LISTS ARE EMPTY AT THIS POINT (BUT I WOULD LIKE TO USE THEM HERE)
}
Problem solved:
As it was brought up by Yogendra Singh and twaddington, the getDataInBackground method was running as a secondary thread which did not have a chance to finish before I was reaching the specific place in my code where I needed the information being retrieved there. As my ultimate task was to dynamically fill up a listView with information retrieved from my Parse database, I simply decided to follow the hints here and do all that from within the getDataInBackground. That worked! Thanks everyone for the help.