0
In my android studio project I have a class of getters and setters to help me, but for some reason on main Activity when I call the db.busca()
(which is in the database operations class) which places a token in the u.setToken
("comes from comic book").
Then on Mainactivity if I give user.getToken()
it comes null, I’ve had to test if I give a user.getToken()
in the Database class everything works ok, but in Mainactivity results null.
In some cases I want it to return null but when it should not return null and yes a token it returns null
Class of the Database:
public void busca(){
String[] colunas = new String[]{"token", "nome"};
Cursor cursor = db.query("user", colunas, null, null, null, null, "token ASC");
if(cursor.getCount() > 0){
cursor.moveToFirst();
do{
Utilizador u = new Utilizador();
u.setToken(cursor.getString(0));
u.setNome(cursor.getString(1));
}while(cursor.moveToNext());
}
}
User class:
package imm.pt.immflix;
public class Utilizador {
private String nome;
private String mail;
private String token;
private String foto;
private int typeService;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getFoto() {
return foto;
}
public void setFoto(String foto) {
this.foto = foto;
}
public int getTypeService() {
return typeService;
}
public void setTypeService(int typeService) {
this.typeService = typeService;
}
}
Mainactivity class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
TextView tvtitle = findViewById(R.id.tvTitle);
ObjectAnimator anime = ObjectAnimator.ofFloat(tvtitle, "alpha", 0f, 1f);
anime.setDuration(2000);
anime.start();
DB db = new DB(this);
db.busca();
Utilizador user = new Utilizador();
token = user.getToken();
if(token == null){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, Login.class);
intent.putExtra("isNew", true);
startActivity(intent);
finish();
}
}, 3000);
}else{
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, Login.class);
intent.putExtra("isNew", false);
startActivity(intent);
finish();
}
}, 3000);
}
}
Thank you, that was the problem, and yes only return a "User" technically returns a token and the name of the user logged into my APP and this token is then checked with the server to perform other actions, thank you again!
– Tomás