开发者

When selecting all rows from android sqlite database for an application, it is returning the last entry for all the entries

开发者 https://www.devze.com 2023-04-10 01:11 出处:网络
I am currently working on an android application.I have a sqlite database that stores text (that I just use as strings in my application) in four columns. I am trying to return all of the rows and col

I am currently working on an android application. I have a sqlite database that stores text (that I just use as strings in my application) in four columns. I am trying to return all of the rows and columns from the table. I have created and inserted data into the table and verified it is there using sqlite3 from the adb shell. I use the same statement as I u开发者_如何学Pythonse in my program and it returns all the rows with all of the correct data. In my program I store all of the data in an ArrayList<ArrayList<String>> format by iterating through the cursor. It returns the correct number of ArrayList<String> that corresponds to the rows, but they all have the information from only the last row. Here is my code:

private static final String SELECT = "SELECT * FROM " + TABLE_NAME;

public ArrayList<ArrayList<String>> allRecipes()
{
    ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
    ArrayList<String> recipe = new ArrayList<String>();
    Cursor cursor = db.rawQuery(SELECT, null);
    if(cursor.moveToFirst())
    {
        do
        {
            recipe.clear();
            recipe.add(cursor.getString(1));
            recipe.add(cursor.getString(2));
            recipe.add(cursor.getString(3));
            recipe.add(cursor.getString(4));
            results.add(recipe);
        }while(cursor.moveToNext());
        if(cursor != null && !cursor.isClosed())
            cursor.close();
    }
    return results;
}

I then iterate through the ArrayLists in another part of my program and all of the information contained is just duplicates of the last row entered into the table. I have checked the ArrayLists in my other method as soon as it receives it, and they are all the same, so I am assuming it must be an issue in this code segment somehow. I have also tried the select statement with group by and order by clauses and it still does not work. Using the db.query() with correct parameters causes the same issues as well.


It happens because you add link to recipe in your array list, and change values of recipe on every iteration in cycle.

You should change code to this one

public ArrayList<ArrayList<String>> allRecipes()
{
   ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
   Cursor cursor = db.rawQuery(SELECT, null);
   if(cursor.moveToFirst())
   {
       do
       {
           ArrayList<String> recipe = new ArrayList<String>();
           recipe.add(cursor.getString(1));
           recipe.add(cursor.getString(2));
           recipe.add(cursor.getString(3));
           recipe.add(cursor.getString(4));
           results.add(recipe);
       }while(cursor.moveToNext());
       if(cursor != null && !cursor.isClosed())
          cursor.close();
   }
   return results;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消