sqllite database returning only last value multiple times [duplicate]
This question already has an answer here:
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
i've been having some trouble with sql lite where the database returns only the last value entered times the number of rows it currently has when I query for it.
I'm trying to retrieve the list of locations from a table that holds them.
public List<Location> getSavedLocations()
{
List<Location> savedLocations = new ArrayList<>();
Location savedLocation = new Location();
Cursor cursor = db.query(
SLTable.TABLE_SL,
SLTable.ALL_COLUMNS,
null,
null,
null,
null,
null);
while(cursor.moveToNext())
{
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
Log.i("AAA1", savedLocation.getId() + " " + savedLocation.getTitle());
}
for(Location l : savedLocations)
{
Log.i("AAA2", l.getId() + " " + l.getTitle());
}
cursor.close();
return savedLocations;
}
Here is an image of the logcat results
Logcat results
last location entered was 7777 so it's displayed for all of them
After i deleted location 7777, it shows the last location before it
android sqlite
marked as duplicate by Mike M.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
i've been having some trouble with sql lite where the database returns only the last value entered times the number of rows it currently has when I query for it.
I'm trying to retrieve the list of locations from a table that holds them.
public List<Location> getSavedLocations()
{
List<Location> savedLocations = new ArrayList<>();
Location savedLocation = new Location();
Cursor cursor = db.query(
SLTable.TABLE_SL,
SLTable.ALL_COLUMNS,
null,
null,
null,
null,
null);
while(cursor.moveToNext())
{
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
Log.i("AAA1", savedLocation.getId() + " " + savedLocation.getTitle());
}
for(Location l : savedLocations)
{
Log.i("AAA2", l.getId() + " " + l.getTitle());
}
cursor.close();
return savedLocations;
}
Here is an image of the logcat results
Logcat results
last location entered was 7777 so it's displayed for all of them
After i deleted location 7777, it shows the last location before it
android sqlite
marked as duplicate by Mike M.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
i've been having some trouble with sql lite where the database returns only the last value entered times the number of rows it currently has when I query for it.
I'm trying to retrieve the list of locations from a table that holds them.
public List<Location> getSavedLocations()
{
List<Location> savedLocations = new ArrayList<>();
Location savedLocation = new Location();
Cursor cursor = db.query(
SLTable.TABLE_SL,
SLTable.ALL_COLUMNS,
null,
null,
null,
null,
null);
while(cursor.moveToNext())
{
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
Log.i("AAA1", savedLocation.getId() + " " + savedLocation.getTitle());
}
for(Location l : savedLocations)
{
Log.i("AAA2", l.getId() + " " + l.getTitle());
}
cursor.close();
return savedLocations;
}
Here is an image of the logcat results
Logcat results
last location entered was 7777 so it's displayed for all of them
After i deleted location 7777, it shows the last location before it
android sqlite
This question already has an answer here:
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
i've been having some trouble with sql lite where the database returns only the last value entered times the number of rows it currently has when I query for it.
I'm trying to retrieve the list of locations from a table that holds them.
public List<Location> getSavedLocations()
{
List<Location> savedLocations = new ArrayList<>();
Location savedLocation = new Location();
Cursor cursor = db.query(
SLTable.TABLE_SL,
SLTable.ALL_COLUMNS,
null,
null,
null,
null,
null);
while(cursor.moveToNext())
{
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
Log.i("AAA1", savedLocation.getId() + " " + savedLocation.getTitle());
}
for(Location l : savedLocations)
{
Log.i("AAA2", l.getId() + " " + l.getTitle());
}
cursor.close();
return savedLocations;
}
Here is an image of the logcat results
Logcat results
last location entered was 7777 so it's displayed for all of them
After i deleted location 7777, it shows the last location before it
This question already has an answer here:
Why does my ArrayList contain N copies of the last item added to the list?
4 answers
android sqlite
android sqlite
asked Nov 21 '18 at 23:41
Zayed AlzaabiZayed Alzaabi
82
82
marked as duplicate by Mike M.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mike M.
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
That's because each time you add a location, you also change the value of location previously assigned. Instead, try creating new instance of Location
when adding it to the list:
while (cursor.moveToNext()) {
Location savedLocation = new Location();
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
That's because each time you add a location, you also change the value of location previously assigned. Instead, try creating new instance of Location
when adding it to the list:
while (cursor.moveToNext()) {
Location savedLocation = new Location();
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
}
add a comment |
That's because each time you add a location, you also change the value of location previously assigned. Instead, try creating new instance of Location
when adding it to the list:
while (cursor.moveToNext()) {
Location savedLocation = new Location();
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
}
add a comment |
That's because each time you add a location, you also change the value of location previously assigned. Instead, try creating new instance of Location
when adding it to the list:
while (cursor.moveToNext()) {
Location savedLocation = new Location();
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
}
That's because each time you add a location, you also change the value of location previously assigned. Instead, try creating new instance of Location
when adding it to the list:
while (cursor.moveToNext()) {
Location savedLocation = new Location();
savedLocation.setId(cursor.getInt(cursor.getColumnIndex(SLTable.COLUMN_ID)));
savedLocation.setTitle(cursor.getString(cursor.getColumnIndex(SLTable.COLUMN_TITLE)));
savedLocation.setLat(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LAT)));
savedLocation.setLng(cursor.getDouble(cursor.getColumnIndex(SLTable.COLUMN_LNG)));
savedLocations.add(savedLocation);
}
answered Nov 21 '18 at 23:57
AaronAaron
1,7032212
1,7032212
add a comment |
add a comment |