Could anyone help me find a way to show the fields of the a new user (newly registered) into an activity of an Android Studio app?

Glaudiston Neto :

I am developing an application that requires the registration of a user, the database (SQLite) has three tables, the cliente table that has everything a usuario table has, a trabalhador table that has two more lines that usuario table does't has and the table usuario that has all the information for the registered users, but I'm not so sure if still need an id to the those two tables, the trabalhador table and the cliente table because if I register any of them, both are gonna be usuario, but I had a doubt when thinking about how to show the newly registered user his newly registered information.

I have a good idea how to show an ArrayList with the information of all users already registered:

public ArrayList<Usuario> getAllUsuarios() {
ArrayList<Usuario> listaUsuarios = new ArrayList<Usuario>();
String query = "SELECT    *  FROM  " + TABELA_USUARIOS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
    do {
        Usuario usuario = cursorToUsuario(cursor); //método criado no banco de dados
        listaUsuarios.add(usuario);

    } while
            (cursor.moveToNext());
}
return listaUsuarios;

}

I also made a method in the database to return an user only:

public Usuario getUsuario(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(
        TABELA_USUARIOS,
        COLUNAS,
        "id = ?",
        new String[]{String.valueOf(id)},
        null, null, null, null);
if (cursor == null) {
    return null;
} else {
    cursor.moveToFirst();
    Usuario usuario = cursorToUsuario(cursor); //método também criado
    return usuario;
}

into an activity to be redirected after the registration, but the problem is to use this method to show the data of the last registered user. I can create an ArrayList to request all registered users in this activity:

    final ArrayList<Usuario> listaUsuarios = bd.getAllUsuarios();
ArrayAdapter<Usuario> adapter = new ArrayAdapter<Usuario>(this,
        android.R.layout.simple_list_item_1, listaUsuarios);
lvListaUsuarios.setAdapter(adapter);

I can show all users in this activity, but I'm having trouble getting the right logic to show only the last user ...

I would be really grateful if anyone could give me any help with this.

ישו אוהב אותך :

You can either using ORDER BY DESC or using cursor.moveToLast().

with ORDER BY DESC:

public Usuario getLastUsuarios() {
    String query = "SELECT    *  FROM  " + TABELA_USUARIOS + " ORDER BY id DESC";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    if (cursor.moveToFirst()) {
        return cursorToUsuario(cursor);
    }

    // not found.
    return null;
}

with cursor.moveToLast();:

public Usuario getLastUsuario() {
   SQLiteDatabase db = this.getReadableDatabase();
   // get all rows in table.
   Cursor cursor = db.query(TABELA_USUARIOS, null, null, null, null, null, null);

   Usuario usuario = null;
   if (cursor != null) {
     cursor.moveToLast();
     usuario = cursorToUsuario(cursor);
   }

   cursor.close();
   return usuario;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=158779&siteId=1