开发者

Database Sorting depending on Integer?

in my database having some row's , in every row having three column's(id, name, moves, time). i want to sort the rows depending on the moves this is the integer value.

i tried like this but unlucky.

public Cursor fetchAllNotes() {
    return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME, 
         KEY_MOVES,KEY_TIME}, null, null, null, null, KEY_MOVES);
}

used like this

private void getallrows()  {
    // get all the contacts..
    db.open();
    Cursor c = db.fetchAllNotes();       
    if (c.moveToFirst())   {        
       do {
          Sorting(c);
       } while (c开发者_开发知识库.moveToNext());
    }
    db.close();
}

moves i have like 15, 45, 21, 11, 10,75,33 i want like this 10,11,15,21,33,45,75


Try this -

return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME, 
         KEY_MOVES,KEY_TIME}, null, null, null, null, KEY_MOVES + " ASC");

or

return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME, 
         KEY_MOVES,KEY_TIME}, null, null, null, null, KEY_MOVES + " DESC");

EDIT - 1:

If you want to display name,you can use like this,

private void getall()
{ 
        db.open(); 
        Cursor c = db.fetchAllNotes();
        int i=1; 
        if (c.moveToFirst())
        {               
            do
            {
                DisplayContact(c.getString(c.getColumnIndex(KEY_NAME))); 
                i++;
            } while (c.moveToNext()); 
        } 
        db.close(); 
}


Why dont u make query in such a way which return the sorted data itself ??? Try out This query :

SELECT * FROM DATABASE_TABLE ORDER BY KEY_MOVES ASC

or

db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME, KEY_MOVES,KEY_TIME}, null, null, null, null, KEY_MOVES + " ASC");

Hope u get it...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜