• 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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;
}
  • 1
Reply Report