简体   繁体   English

谁能帮我找到一种方法来将新用户(新注册)的字段显示到 Android Studio 应用程序的活动中?

[英]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?

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.我正在开发一个需要注册用户的应用程序,数据库 (SQLite) 有三个表,一个包含 usuario 表所有内容的 cliente 表,一个包含 usuario 表没有的另外两行的 trabalhador 表,以及包含注册用户所有信息的 usuario 表,但我不确定是否还需要这两个表的 id,trabalhador 表和 cliente 表,因为如果我注册其中任何一个,两者都会成为 usuario ,但是在考虑如何向新注册用户展示他的新注册信息时,我有一个疑问。

I have a good idea how to show an ArrayList with the information of all users already registered:我有一个好主意如何显示一个包含所有已注册用户信息的 ArrayList:

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.进入一个注册后要重定向的activity,但是问题是用这个方法来显示上次注册用户的数据。 I can create an ArrayList to request all registered users in this activity:我可以创建一个 ArrayList 来请求此活动中的所有注册用户:

    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() .您可以使用ORDER BY DESC或使用cursor.moveToLast()

with ORDER BY DESC :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();使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM