-1

I have this app which gets users data from the database using the username they logged in with. The user name is passed once they login to the page which retrieves their account information. The app crashes saying retrieving the information was wrong, can anyone help?

The database code

public Cursor RetriveLoggedUsersData (DatabasOpperations DBOpp, String Username){
    SQLiteDatabase SQDB = DBOpp.getReadableDatabase();
    String[] Coloumns = {TableData.TableInfo.FIRSTNAME, TableData.TableInfo.LASTNAME, TableData.TableInfo.EMAIL, TableData.TableInfo.USERNAME, TableData.TableInfo.PASSWORD, TableData.TableInfo.IMAGE};
    String Where = TableData.TableInfo.USERNAME + " LIKE ?";
    String Argument[] = {Username};
    Cursor Cur = SQDB.query(TableData.TableInfo.TABLE_NAME, Coloumns, Where, Argument, null, null, null);
    Log.d("DatabaseOperations", "Success, User Retrived");
    return Cur;
}

The code which wants to retrieve the data

Username = getIntent().getExtras().getString("Username");

DatabasOpperations DB = new DatabasOpperations(Contx);
        Cursor Cur = DB.RetriveLoggedUsersData(DB, Username);
        DBFName = Cur.getString(0);
        DBLName = Cur.getString(1);
        DBEmail = Cur.getString(2);
RexDough
  • 181
  • 1
  • 11
  • When your app has a crash, the logcat output is relevant when posting your question so it becomes easier to track the problem. – Steve Kamau Apr 23 '16 at 05:22

1 Answers1

2

You need to move the Cursor to the first position by making a call to moveToFirst(), and also check the return value to make sure there is at least one row in the Cursor. Also be sure to close your Cursor:

Cursor Cur = DB.RetriveLoggedUsersData(DB, Username);
if (Cur.moveToFirst()) {
    DBFName = Cur.getString(0);
    DBLName = Cur.getString(1);
    DBEmail = Cur.getString(2);
}
Cur.close();
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137