Android sqlite - Return _id logged in Listview only one value selected using Where condition

Asked

Viewed 993 times

0

Good evening guys, I’m new here, I’m new to Android programming, but I decided to do my TCC, a project on Android. by doing alone, finding material on the Internet, without taking Android lessons, I’m going as well (or not), but stranded at a certain point.

I have a Listview that returns the data of the Sqlite database.

If I leave SELECT * FROM tbl_usuario; it brings me all registered users in the database, but I just wanted to let appear the user who logged in to the system.

i.e., I wanted to make a condition within Select, being "SELECT * FROM tbl_usuario WHERE _id = id that is logged in to the system.

but I can’t, I’ve already spent two days looking for a way to do, I’ve changed the structure of the code and I’m lost.

Well, my code is there.

public class Area_Usuario extends ActionBarActivity{
private String TABLE;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.area_usuario);
    TABLE = "tbl_usuario";


    DatabaseHandler db = new DatabaseHandler(this);
    Usuario usuario = new Usuario();
    SQLiteDatabase db1 = db.getReadableDatabase();
    String sql = ("SELECT * FROM " + TABLE);
    Cursor cursor= db1.rawQuery(sql,new String[]{String.valueOf(usuario.get_id())});
    String[] from = {"nome", "email", "senha"};
    int[] to = {R.id.Usuario_RowNome, R.id.Usuario_RowEmail, R.id.Usuario_RowSenha};
    ListView ltUsuario = (ListView) findViewById(R.id.listViewDadosUsuario);
    SimpleCursorAdapter ad = new SimpleCursorAdapter(getBaseContext(), R.layout.usuario_row, cursor, from, to, 1);
    ltUsuario.setAdapter(ad);
    db1.close();
   }
}

1 answer

1


If you want to use the clause WHERE you will need to indicate it in Query:

String sql = ("SELECT * FROM " + TABLE + " WHERE _id = ?");
Cursor cursor= db1.rawQuery(sql,new String[]{String.valueOf(usuario.get_id())});

The ? is a 'placeholder' to receive the amount indicated in new String[]{String.valueOf(usuario.get_id())}

I suggest that as an alternative to db rawQuery., use the methods that Sqlitedatabase available for operations CRUD.

In the case of data reading use one of the variants of Sqlitedatabase.query

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs,
                    String groupBy, String having, String orderBy)

Would look like this:

Cursor cursor = db1.query(TABLE, null, "_id = ?",
                           new String[]{String.valueOf(usuario.get_id())},null,null,null);
  • ramaral, I understood this way of using Cursor, I even thought better, I type user and password, and log in, but still my Listview appears blank, I mean, I believe it is not loading the data from. String[] from = {..... }; int[] to = {.....};

  • You’re instantiating the class User and does not attribute any value to it, so usuario.get_id() will always return 0. If you do not know the user id you will have to search, in the table, the user by name and not by id.

  • I don’t understand why you want to use one Listview when the result of the query will only be a record.

  • I can return the user data in a simple Textview (in case a Textview for each returned data)??

  • of course you can. Use the methods getInt(int columnIndex), getDouble(int columnIndex), getString(int columnIndex), etc, to obtain the data for each column.

  • It was not indicated in my previous comment that the methods I refer to are of the class Cursor

  • I managed to solve my problem, instead of I bring the user’s data by Id, I decided to bring by email, but I need only now, to be able to make a check if the email is already registered in the database, so that the user can not do twice the registration of the same email. put the email in putExtra on screen 1 and on screen 2 I received the email with getExtra and made the query using the email received from screen 1.

  • This is the usual way to check if a user is registered, when the username is the email. Don’t forget to check the password. The whole username and password is what identifies a user.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.