2014-04-14 81 views
0

目前下面的代碼顯示數據庫中的所有行,但我想將其更改爲僅顯示最後一列和最後一列。爲了做到這一點,我需要添加和更改哪些內容?如何只顯示最後一行?

public String getData() 
{ 
    // TODO Auto-generated method stub 
    String[] columns = new String[] {KEY_ROWID, KEY_FNAME, KEY_SNAME, 
    KEY_HOURS, KEY_REG, KEY_CPARK, KEY_TIMESTAMP}; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, 
              null, null); 
    String result = " "; 

    int iRow = c.getColumnIndexOrThrow(KEY_ROWID); 
    int iFname = c.getColumnIndexOrThrow(KEY_FNAME); 
    int iSname = c.getColumnIndexOrThrow(KEY_SNAME); 
    int iHours = c.getColumnIndexOrThrow(KEY_HOURS); 
    int iReg = c.getColumnIndexOrThrow(KEY_REG); 
    int iCpark = c.getColumnIndexOrThrow(KEY_CPARK); 
    int iTime = c.getColumnIndexOrThrow(KEY_TIMESTAMP); 

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + c.getString(iRow) + " " + c.getString(iFname) + " 
        " + c.getString(iSname) + " " + c.getString(iHours) + " " + 
     c.getString(iReg) + " " + c.getString(iCpark) + " " + 
        c.getString(iTime) + "\n"; 
    } 
    return result; 
} 
+0

最後一行還是最後一列?你的問題並不清楚。 – njzk2

回答

3

要顯示最後一行使用下面的代碼,您對最新的列有什麼意思?

public String getData() 
{ 
    // TODO Auto-generated method stub 
    String[] columns = new String[] {KEY_ROWID, KEY_FNAME, KEY_SNAME, 
    KEY_HOURS, KEY_REG, KEY_CPARK, KEY_TIMESTAMP}; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, 
              null, null); 
    String result = " "; 

    int iRow = c.getColumnIndexOrThrow(KEY_ROWID); 
    int iFname = c.getColumnIndexOrThrow(KEY_FNAME); 
    int iSname = c.getColumnIndexOrThrow(KEY_SNAME); 
    int iHours = c.getColumnIndexOrThrow(KEY_HOURS); 
    int iReg = c.getColumnIndexOrThrow(KEY_REG); 
    int iCpark = c.getColumnIndexOrThrow(KEY_CPARK); 
    int iTime = c.getColumnIndexOrThrow(KEY_TIMESTAMP); 

    if (c.moveToLast()) { 
     result = result + c.getString(iRow) + " " + c.getString(iFname) + " 
        " + c.getString(iSname) + " " + c.getString(iHours) + " " + 
     c.getString(iReg) + " " + c.getString(iCpark) + " " + 
        c.getString(iTime) + "\n"; 
     return result; 
    } else { 
     return null; //no row selected 
    } 
} 
+0

謝謝!完美的作品。 – user3408604

+0

請接受答案,讓其他人首先看到答案,也可以從解決方案中受益。 – speedy1034