Interesting.

The docs specifically say that query() returns "A Cursor object, which is positioned before the first entry." You can test by calling cursor.getPosition(), it should be "-1".

Are you moving the cursor *before* that processing loop? (calling any move() methods?)

If you're not, then that looks to be a side effect of using a row limit.

-- Kostya

10.02.2011 15:00, André пишет:
Thanks Kostya,

I did as you suggested and it looks much better.
For my question though I got the same problem when using your code.
But I tried adding:
String item1 = cursor.getString(index1);
String item2 = cursor.getString(index2);

before while(cursor.moveToNext()){
as well as in it. And then it works fine. I guess it skips the first
post in cursor when I call moveToNext in while. And it probably did
the same in my old code.

//André

On Feb 10, 12:18 pm, Kostya Vasilyev<kmans...@gmail.com>  wrote:
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


--
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

Reply via email to