1

I have tried many different ways to do this, but basically when I get all of data from my database I want to store it in an array, but I do not know how to iterate through the index's as the data is read from the database. Here is how I am getting the data and adding it to the array:

public void makeObjects(){
    i =0;
    db.open();
    for(int j = 0; j< amount; j++)
    {
        objarray.add(o); 
    }

    Cursor c = db.fetchAllNotes();
    if (c.moveToFirst())
    {

        do {      

            objarray.get(i).setid(c.getString(0));
            objarray.get(i).settitle(c.getString(1));
            objarray.get(i).setinfo(c.getString(2));
            objarray.get(i).setlat(c.getString(3));
            objarray.get(i).setlon(c.getString(4));
            objarray.get(i).setmc(c.getString(5));

            i++;
        } while (c.moveToNext());
    }



    db.close();

}

For testing and simplicity I have two rows in my database, I have a for loop which adds two new indexes to the array, but when I print out the contents of the array to the logcat, I see that both indexes have the last data to be read from the database, the second row twice. How can I make sure that row one enters index 0 and row two enters index 1?

3 Answers 3

2

You need to create a new object for each element of your array to reference, rather than having them all reference the same object.

Sign up to request clarification or add additional context in comments.

3 Comments

That actually makes a lot of sense, could you show me how to do this please? This could take me a long time to figure out. Thank you!!!!
How are you initializing o? Probably, the right hand side of that initialization is what you should be using inside the first loop. Something like objarray.add(new WhatEver());
You were right, all this time I was missing this little piece of code and I tried everything but that! :( Thank you for noticing this and helping me solve this, it all works now, I will accept this as the answer once it allows me to! :)
2

I have tried many different ways to do this, but basically when I get all of data from my database I want to store it in an array

What about to create own Object that presents table in your db? This Object will have some attributes which will present columns in table.

And here is algorithm how to save each row from db into List of Objects:

  1. Fetch all rows into Cursor
  2. Loop through Cursor and for each row create new Object and save it into List of Objects.

Example:

public class Foo {

   private int id;
   private String name;

   // getters and setters
}

Now implementation of method:

public List<Foo> getAll() {
   List<Foo> foos = new ArrayList<Foo>();
   Foo member = null;
   Cursor c = null;
   try {
      c = db.rawQuery("Select * from Table", null);
      if (c.moveToFirst()) {
         do {
            member = new Foo();
            member.setId(c.getInt(c.getColumnIndex("id")));
            member.setName(c.getString(c.getColumnIndex("name")));
            foos.add(member);
         } while (c.moveToNext());
      }
      return foos;
   }
   finally {
      if (c != null) {
         c.close();
      }
   }
}

1 Comment

Hey buddy!! Thanks for your answer, I managed to fix it, I was missing a little piece of code all this time!! :( :) Excellent answer though, thank you I will keep this code!
1

I hope this might help you. This function reads the database and returns the row as an object. This function is wrtten in a class which extends SQLiteOpenHelper.

ParamsBMI getTemplate(String strPersonName)
{
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_BMI_PARAMS, new String[] { KEY_PERSON_NAME, KEY_GENDER_MALE, KEY_DOB_YEAR, KEY_DOB_MOY, KEY_DOB_DOM, KEY_HEIGHT_UNIT, KEY_WEIGHT_UNIT, KEY_HEIGHT_CMS, KEY_HEIGHT_FT, KEY_HEIGHT_INCHES, KEY_WEIGHT_KGS,
            KEY_WEIGHT_POUNDS }, KEY_PERSON_NAME + "=?", new String[] { strPersonName }, null, null, null, null);

    if (cursor != null)
        cursor.moveToFirst();

    ParamsBMI paramsBMI = new ParamsBMI();

    paramsBMI.setPersonName(cursor.getString(0));

    boolean bGenderMale = (cursor.getInt(1) == 1) ? true : false;
    paramsBMI.setGenderMale(bGenderMale);

    paramsBMI.setDOBYear(Integer.parseInt(cursor.getString(2)));
    paramsBMI.setDOBMonthOfYear(Integer.parseInt(cursor.getString(3)));
    paramsBMI.setDOBDayOfMonth(Integer.parseInt(cursor.getString(4)));

    paramsBMI.setHeightUnit(Integer.parseInt(cursor.getString(5)));
    paramsBMI.setWeightUnit(Integer.parseInt(cursor.getString(6)));

    paramsBMI.setHeightCms(Float.parseFloat(cursor.getString(7)));
    paramsBMI.setHeightFt(Float.parseFloat(cursor.getString(8)));
    paramsBMI.setHeightInches(Float.parseFloat(cursor.getString(9)));

    paramsBMI.setWeightKgs(Float.parseFloat(cursor.getString(10)));
    paramsBMI.setWeightPounds(Float.parseFloat(cursor.getString(11)));

    return paramsBMI;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.