fetch all records from a sqlite column

Asked

Viewed 135 times

0

I need to save in a list the names of the user table and save them in an Arraylist. I did as follows:

 public List<String> buscarUsuarios() {
    List<String> nomes = new ArrayList<String>();
    String selectQuery = "select nome from usuarios";
    Cursor cursor = db.rawQuery(selectQuery, null);


    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        do {

            cursor.moveToFirst();
            String nomeString = cursor.getString(cursor.getColumnIndex("nome"));

            StringBuilder conversor = new StringBuilder();
            conversor.append(nomeString);

            nomes.add(conversor.toString());

        } while (cursor.moveToNext());

    }
    return nomes;
}

But it’s looping endlessly Someone can help me?

  • 2

    You are always moving to the first, in all the loops. So, it will never reach the end

  • Ours is the lack of coffee. THANK YOU!

  • although remove works, it would be best to Voce position it outside the loop.

1 answer

3


You are moving the cursor to the first record every time.

Removes the cursor.moveToFirst(); from within the do

Browser other questions tagged

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