2014-04-14 79 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