Results 1 to 7 of 7
Thread: Java/Android Question
- 02-22-2013, 12:06 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 1
- Rep Power
- 0
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!
- 02-22-2013, 04:47 AM #2
Re: Java/Android Question
Show the code that constructs the data objects and adds them to the ArrayList.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-22-2013, 09:26 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 02-22-2013, 04:36 PM #4
Banned
- Join Date
- Feb 2013
- Posts
- 1
- Rep Power
- 0
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.
- 02-22-2013, 04:56 PM #5
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
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()); } }
- 02-22-2013, 06:06 PM #6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-25-2013, 10:35 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Android Java NewB Question...
By dmagician in forum AndroidReplies: 0Last Post: 06-25-2012, 11:32 AM -
Java and Android App's.
By bjornlindekens in forum AndroidReplies: 2Last Post: 06-25-2012, 10:55 AM -
New to android, probably a stupid question
By mwr1976 in forum AndroidReplies: 0Last Post: 11-22-2011, 08:47 PM -
What can be the question to be asked for android developers
By rajan701v in forum AndroidReplies: 0Last Post: 06-03-2011, 10:22 AM -
Java android
By amandricel in forum New To JavaReplies: 0Last Post: 04-21-2011, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks