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 8139691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:54:51+00:00 2026-06-06T11:54:51+00:00

I am working on an android project and I am facing a problem and

  • 0

I am working on an android project and I am facing a problem and the problem is:

Arraylist empty when I return it.

Here is my java code:

 ArrayList<ArrayList<Object>> container = new ArrayList<ArrayList<Object>>();
            ArrayList<Object> itemRow = new ArrayList<Object>();
            JSONObject jsonObj =  new JSONObject(result);
            JSONArray allElements = jsonObj.getJSONArray("Table");
            Log.i("allElements", "" + allElements);
            for (int i = 0; i < allElements.length(); i++) {
                itemRow.add(allElements.getJSONObject(i).getString("ParentName").toString());
                itemRow.add(allElements.getJSONObject(i).getString("ParentEmailID").toString());
                itemRow.add(allElements.getJSONObject(i).getString("ParentContact").toString());
                itemRow.add(allElements.getJSONObject(i).getString("ParentAddress").toString());
                itemRow.add(allElements.getJSONObject(i).getString("ParentProfilePictureName").toString());
                itemRow.add(allElements.getJSONObject(i).getString("StudentName").toString());
                Log.i("itemRow", "itemRow at index: " + i + ", " + itemRow);
                container.add(((i*2)/2), itemRow);
                itemRow.clear();
            }

            return container;

In this code I have two Arraylist one for contain all the elements and another one for storing single row of elements. These Arraylist are loaded from JSONArray, all is working fine and I can print data from item row (Arraylist which take single row) and store into main Arraylist (container).

But when I return this Arraylist (container) and print in logcat it shows empty Arraylist like

[[], [], [], [], []].

I cannot understand why this happen please help me to solve this issue.

Thanks.

  • 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-06T11:54:53+00:00Added an answer on June 6, 2026 at 11:54 am

    Stop clearing the list, and it won’t be empty anymore:

    itemRow.clear();
    

    You should create a new list at each iteration. Put the following line of code inside the for loop:

    ArrayList<Object> itemRow = new ArrayList<Object>();
    

    Remember that Java passes references to objects. So the container list holds a reference to the list you add to it. It doesn’t make a copy of the list. So your current code adds several references to the same list object to the container list, and you clear the list each time you add it. It thus contains N references to the same empty list at the end of the loop.

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

Sidebar

Related Questions

I am new to Sockets, Working on a project with C# host and Java/Android
i am working on Android project using Eclipse Eclipse IDE for Java Developers Version:
I am working on an Android project and have a problem with SQLite database
I'm working on an android project that involves native code and I'm trying to
I am currently working on android project where I am using a custom list
I am currently working on android project where I want to have a text
i am working on a android project for my assignment. i am trying to
I am working on an android project where I am using a ListView and
I am working on an android project and I am using a spinner which
I was working on my android project. After completing some coding, I imported remaining

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.