sqllite database returning only last value multiple times [duplicate]












0
















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










share|improve this question













marked as duplicate by Mike M. android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

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.























    0
















    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










    share|improve this question













    marked as duplicate by Mike M. android
    Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

    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.





















      0












      0








      0


      0







      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










      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 23:41









      Zayed AlzaabiZayed Alzaabi

      82




      82




      marked as duplicate by Mike M. android
      Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

      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. android
      Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

      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.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          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);
          }





          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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);
            }





            share|improve this answer




























              0














              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);
              }





              share|improve this answer


























                0












                0








                0







                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);
                }





                share|improve this answer













                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);
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 23:57









                AaronAaron

                1,8282213




                1,8282213

















                    Popular posts from this blog

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?