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();
}
}
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 = {.....};
– Nardy Lenis
You’re instantiating the class User and does not attribute any value to it, so
usuario.get_id()
will always return0
. If you do not know the user id you will have to search, in the table, the user by name and not by id.– ramaral
I don’t understand why you want to use one Listview when the result of the query will only be a record.
– ramaral
I can return the user data in a simple Textview (in case a Textview for each returned data)??
– Nardy Lenis
of course you can. Use the methods
getInt(int columnIndex)
,getDouble(int columnIndex)
,getString(int columnIndex)
, etc, to obtain the data for each column.– ramaral
It was not indicated in my previous comment that the methods I refer to are of the class Cursor
– ramaral
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.
– Nardy Lenis
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.
– ramaral