List always returns only the first row of the sqlite database

Asked

Viewed 416 times

0

I have a list, which shows all the data of the sqlite database, image path, latitude and longitude, but in the list only changes the image path, latitude and longitude always remains those of the first record.

public List<Foto> todasFotos(){

    List<Foto> listaFotos = new ArrayList<Foto>();

    SQLiteDatabase db =  getReadableDatabase();

    String sqlTodasFotos =  "SELECT * FROM imagens";

    Cursor c = db.rawQuery(sqlTodasFotos,null);

    if (c.moveToNext()) {
        do {
            Foto foto = new Foto();
            foto.setId(c.getInt(0));
            foto.setCaminho_foto(c.getString(1));
            foto.setLatitude(c.getDouble(2));
            foto.setLongitude(c.getDouble(3));

            listaFotos.add(foto);
        } while (c.moveToNext());
    }
    db.close();
    return listaFotos;
}
  • 2

    Make sure the problem isn’t when you recorded the records.

  • 1

    I checked and it’s actually saving the same latitude and longitude for all the records, the weird one that was working yesterday... But thank you so much for the tip.

  • Could you add how you do Insert? As @ramaral said, the problem may be when you saved!

  • Try to get the Photo photo = new Photo(); from inside the looping

  • Try using this SQL: SELECT COUNT(*) FROM imagens and then checks the number that returns in c.getInt(0), this is the number of records in your bank.

  • Juliano, have you solved your problem with the answer? Or do you need some more information?

  • I did, thank you very much !

Show 2 more comments

2 answers

3

Use moveToFirst(). Considering the documentation of Sqlitedatabase it is said that:

A Cursor object, which is positioned before the first entry.

The moveToFirst() does two things:

  1. Allows testing whether the query returned an empty set;
  2. Move the cursor to the first result (when the device is not empty).

Considering the moveToNext():

while (cursor.moveToNext());

The cursor starts before the first line of the result, so in the first iteration it moves to the first result if it exists. If the cursor is empty, or the last line has already been processed, then the loop come out neatly.

The end result would be this way:

if (c.moveToFirst()) {
        do {
            Foto foto = new Foto();
            foto.setId(c.getInt(0));
            foto.setCaminho_foto(c.getString(1));
            foto.setLatitude(c.getDouble(2));
            foto.setLongitude(c.getDouble(3));

            listaFotos.add(foto);
        } while (c.moveToNext());
}

Details

  • I didn’t see anything wrong with @seamusd’s reply, I didn’t understand why -1 should be used moveToFirst() in if same.. + 1

  • @Marcogiovanni It happens all the time! I even took a discussion to the Meta to know how to deal with this kind of situation. And in the answer our colleague said: "Don’t worry too much about it, it’s part of it. I get a lot of negative that I don’t understand why."...

  • From the questions I posted 2 had answers. From the answers I give I get negative. kkkkkkkkkkkkkkkk

  • @Reginaldorigo I believe that negative ends up being a matter of opinion when one is not professional. Sometimes the person does not go with his face and puts the (-1) without thinking twice.

  • @Reginaldorigo the important thing is to try to help, to do our part without looking who; that’s the concept of community.

  • There’s nothing wrong with the content of your answer, the question is that it doesn’t answer the PA’s question. The question code, although not the most commonly used to navigate a cursor, works, see comments.

  • @Reginaldorigo You are right, the important thing is to help, the problem is that these answers do not help the AP. The help and the resolution was given in the first comment, there in the question.

Show 2 more comments

1

Look at it this way:

public List<Foto> todasFotos(){

    List<Foto> listaFotos = new ArrayList<Foto>();

    SQLiteDatabase db =  getReadableDatabase();

    String sqlTodasFotos =  "SELECT * FROM imagens";

    Cursor c = db.rawQuery(sqlTodasFotos,null);
    cursor.moveToFirst();

     if (!cursor.isAfterLast()) {
               do {
                   Foto foto = new Foto();
                   foto.setId(c.getInt(0));
                   foto.setCaminho_foto(c.getString(1));
                   foto.setLatitude(c.getDouble(2));
                   foto.setLongitude(c.getDouble(3));

                    listaFotos.add(foto);
                 }
                  while (cursor.moveToNext());

        }

    db.close();
    return listaFotos;
}
  • Why the negative vote?

  • I did not deny, but I did not, because I do not know the !cursor.isAfterLast(), I think it was the same person who denied the answer of seamusd

  • 1

    Serves to test if the cursor is not empty. If although you have issued the moveToFirst command the pointer is still beyond the end of the dataset then it is certain that the cursor is empty.

  • 1

    There’s nothing wrong with the content of your answer, the question is that it doesn’t answer the PA’s question. The question code, although not the most commonly used to navigate a cursor, works, see comments.

  • To answer his question it is necessary to correct his code.

  • His code works. The problem was saving.

  • Forgive me for insisting. He said in the title of the question that the problem was in the fact of always returning the first line I did not read that the problem was in the recording. I understood that I was returning only the first line because it was not positioning correctly on the cursor.

  • Yes, but there is nothing wrong with his code. Giving an answer to modify the code(that works) does not solve the problem.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.