[android-developers] Probelm with getting posts from database

2011-02-10 Thread André
I have a listview that gets it's posts from a database. I want the ten
latest entries sorted by the time they where added to the database.
This works, but it always skips the first post until a newer one is
added. What could I be doing wrong?

For this I use the following code:

public Cursor fetchAll() {
Cursor mCursor = mDb.query(_TABLE, new String[] {_ROWID,
_NAME, _TEXT, _TIME}, null, null, null, null, _TIME +  DESC, 
10);
return mCursor;
}

Cursor cursor = db.fetchAll();
startManagingCursor(cursor);

ArrayList strings = new ArrayList();
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
String name = cursor.getString(cursor.getColumnIndex(db._NAME));
strings.add(name);
 }
 String[] mNames = (String[]) strings.toArray(new
String[strings.size()]);

 ArrayList strings1 = new ArrayList();
  for(cursor.moveToFirst(); cursor.moveToNext();cursor.isAfterLast())
{
String name =
cursor.getString(cursor.getColumnIndex(db._TEXT));
strings1.add(name);
 }
 String[] mPath = (String[]) strings1.toArray(new
String[strings1.size()]);

// André

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Probelm with getting posts from database

2011-02-10 Thread Kostya Vasilyev

This for loop looks really strange:

for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast())


The condition for isAfterLast should be inverted.

Also don't iterate twice and call getColumnIndex for each row, nether of those 
is necessary. Something like this should work:

cursor = ...
if (cursor != null) {
int index1 = cursor.getColumnIndex(...);
int index2 = cursor.getColumnIndex(...);

while (cursor.moveToNext()) {
String item1 = cursor.getString(index1);
String item2 = cursor.getString(index2);

}
}

Other than that, step through in the debugger, see what values you're 
storing and getting.


Perhaps the top entry somehow has time set to NULL, so it doesn't sort 
properly. You can check this by changing your sort to be _ID DESC (the 
inverse of insertion order) and checking the time value you get back.


-- Kostya

10.02.2011 13:54, André пишет:

I have a listview that gets it's posts from a database. I want the ten
latest entries sorted by the time they where added to the database.
This works, but it always skips the first post until a newer one is
added. What could I be doing wrong?

For this I use the following code:

public Cursor fetchAll() {
 Cursor mCursor = mDb.query(_TABLE, new String[] {_ROWID,
_NAME, _TEXT, _TIME}, null, null, null, null, _TIME +  DESC, 
10);
 return mCursor;
}

Cursor cursor = db.fetchAll();
startManagingCursor(cursor);

ArrayList strings = new ArrayList();
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
String name = cursor.getString(cursor.getColumnIndex(db._NAME));
 strings.add(name);
  }
  String[] mNames = (String[]) strings.toArray(new
String[strings.size()]);

  ArrayList strings1 = new ArrayList();
   for(cursor.moveToFirst(); cursor.moveToNext();cursor.isAfterLast())
{
String name =
cursor.getString(cursor.getColumnIndex(db._TEXT));
strings1.add(name);
  }
  String[] mPath = (String[]) strings1.toArray(new
String[strings1.size()]);

// André




--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en