Results 1 to 7 of 7
  1. #1
    javabiker is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Angry Java/Android Question

    I've been coding in Java since about 1990, but retired some 3 years ago. I recently got into developing apps for the Android platform and now have a very basic question, so basic I risk being laughed at. I get a cursor from a SQLite3 table that contains 3 rows. I extract the content of each cursor row into an data object and add this object to an ArrayList. So now my ArrayList contains 3 objects. In debug I can see that the 3 objects correspond to each table row, so OK. However, when I iterate through the list all I get is 3 objects in which the content of each is the last DO in the list. I've tried using an iterator, do-while and a for-loop and they're all the same. I've written tests and they all confirm this scenario. So, I give up! Here's the code I use to iterate the list.

    private void displayDataObjects(ArrayList<TestDO> objects) {
    TestDO testDO = null;
    System.out.println("List has: " + objects.size() + " entries");
    Iterator<TestDO> list1 = objects.iterator();

    while (list1.hasNext()) {
    // testDO = list1.next();
    System.out.println((list1.next()).getNAME());
    }
    }
    ..getNAME always shows the last entry in the List!!
    I've confirmed that when I call this method I have 3 distinct DO's containing the correct data. BTW: I've tried ListIterator and Vector.
    What the heck am I missing?
    Any help appreciated!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: Java/Android Question

    Show the code that constructs the data objects and adds them to the ArrayList.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Java/Android Question

    How (and where) did you confirm that there are three distinct objects there?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    klondike is offline Banned
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Angry ArrayList Iteration Issue

    I'm writing some Android apps that use a SQLite DB. After getting some table rows into a Cursor, extracting each row's data into a Data Object (Properties correspond to table row columns), then assigning each DO to an ArrayList I encounter a problem.
    Iterating thru the ArrayList to get my data out of each DO (for reporting purposes) I get the data from the last row in all of the report rows.
    Here's the code:

    private void displayDataObjects(ArrayList<TestDO> objects) {
    System.out.println("ArrayList: " + objects.size() + " entries");
    Iterator<TestDO> list = objects.iterator();

    while (list.hasNext()) {
    // testDO = list.next();
    System.out.println((list.next()).getNAME());
    }
    }

    The name is the same for all rows. I've inspected the contents of each DO as it is assigned to the ArrayList and it's all correct. So what am I doing wrong in the iteration?
    Oh, and I've substituted for-loop, do-while etc and get the same result.

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: ArrayList Iteration Issue

    Not 100% sure on your problem, but try rewriting it like this and see if it helps:

    Java Code:
    private void displayDataObjects(List<TestDO> list)
    {
    	for(TestDO o : list)
    	{
    		System.out.println(o.getNAME());
    	}
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: Java/Android Question

    1. Why the second account?
    2. Please go through the Forum Rules, particularly the second and third paragraphs.

    I've closed the 'klondike' account.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: ArrayList Iteration Issue

    Quote Originally Posted by klondike View Post
    The name is the same for all rows. I've inspected the contents of each DO as it is assigned to the ArrayList and it's all correct. So what am I doing wrong in the iteration?
    Oh, and I've substituted for-loop, do-while etc and get the same result.
    Darryl has already asked you to show us the code that produces the List.
    That's where your problem lies.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Android Java NewB Question...
    By dmagician in forum Android
    Replies: 0
    Last Post: 06-25-2012, 11:32 AM
  2. Java and Android App's.
    By bjornlindekens in forum Android
    Replies: 2
    Last Post: 06-25-2012, 10:55 AM
  3. New to android, probably a stupid question
    By mwr1976 in forum Android
    Replies: 0
    Last Post: 11-22-2011, 08:47 PM
  4. Replies: 0
    Last Post: 06-03-2011, 10:22 AM
  5. Java android
    By amandricel in forum New To Java
    Replies: 0
    Last Post: 04-21-2011, 07:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •