Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:27:14+00:00 2026-06-14T02:27:14+00:00

I would like to add elements to the three ArrayLists declared in the code

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:27:15+00:00Added an answer on June 14, 2026 at 2:27 am

    You have declared the Lists as Class member attributes. These attributes can be directly accessed within any non-static method of your class. You are doing fine in using them in the onCreate method.

    One observatory note: You may want to define the variables as private.

    There may be some other issue which you are correlating the scope of the variables. If you think there is some issue, please share the specific issue being observed.

    EDIT: As suspected, this issue is due to the fact that the lists are getting populated in the background. By the time, it reaches the last line (to print the size), its quite possible that lists are not populated yet.

    PLEASE NOTE: A GetCallback is used to run code after a ParseQuery is used to fetch a ParseObject in a background thread. This means that GetCallBack is being run the background (asynchronous mode).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some forms and I would like to add classes to the elements.
I would like to know how I can add ImageView Elements to a Region
I would like to add an element to jquery.colorbox.js(want to do some fancey styling)
I am using UISearchDisplayController to search and would like to add so more elements
I would like to add to an element's position a dynamic amount e.g. 40px
I would like to add a class which renders an element a particular color
I have an img element with style='width:40%;height:40%;' . I would like to add a
I have own project and i would like add for this class same as
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.