Search code examples
androidandroid-sqlite

Retrieving User's data from database on the basis of email


I have different User's data in a database and I want to retrieve the data the data of the user who is currently logged in to the app, I am able to retrieve the data but I don't know how to display/show it in the main Activity.

Here is my getUserData method

DBHandler

 public ArrayList<User> getUserData(String email){
 Log.e("email", "" + email);
 ArrayList<User> arrayList = new ArrayList<>();
 SQLiteDatabase db = this.getWritableDatabase();
 Cursor cursor;
    String query =  " select * from " +TABLE_NAME+ " where "+COLUMN_EMAIL+ "=?";
  String[] select = new String[]{email};
    cursor = db.rawQuery(query,select);
 Log.e("Query", "" + query);

    if (cursor.moveToFirst()) {
        do {
            String FirstName = cursor.getString(0);
            String LastName = cursor.getString(1);
            String Email = cursor.getString(2);
            byte[] image = cursor.getBlob(5);
            String gender = cursor.getString(6);
            String status = cursor.getString(8);

            User user = new User(FirstName, LastName, Email, image, gender, status);
            arrayList.add(user);
        }

        while (cursor.moveToNext());
    }
    cursor.close();

    return arrayList;}

Now i want to show it in the mainActivity how i can do that?


Solution

  •     TextView FirstName = findViewById(R.id.FirstName);
        TextView LastName = findViewById(R.id.LastName);
    
        ArrayList<User>  userArrayList = new ArrayList<User>(getUserData("user@gmail.com"));  // User data stored in userArrayList now you can use userArrayList for showing data
        
        FirstName.setText(userArrayList.get(0).getFirstName());  // get(0) Gives you first name from first position
        LastName.setText(userArrayList.get(0).getLastName());