0
I have a model and I want to use the "Sharedpreferences" inside it.
To instantiate the SharedPreferences
, i need context(context), as I do to recover that context?
public class PostagemCurtida {
public Feed feed;
public Usuario usuario;
public int qtdCurtidas = 0;
private Context context;
public PostagemCurtida() {
}
final Preferencias preferencias = new Preferencias(context);
public void salvar() {
DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebase();
String usuarioCodificado = Base64Custom.codificarBase64(usuario.getEmail());
HashMap<String, Object> dadosUsuario = new HashMap<>();
dadosUsuario.put("nomeUsuario", usuario.getNome());
dadosUsuario.put("caminhoFoto", usuario.getCaminhoFoto());
DatabaseReference pCurtidasRef = firebaseRef.child("postagens-curtidas").child(preferencias.getFeedId()).child(usuarioCodificado);
pCurtidasRef.setValue(dadosUsuario);
atualizarQtde(1);
}
public void remover() {
DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebase();
String usuarioCodificado = Base64Custom.codificarBase64(usuario.getEmail());
DatabaseReference pCurtidasRef = firebaseRef.child("postagens-curtidas").child(preferencias.getFeedId()).child(usuarioCodificado);
pCurtidasRef.removeValue();
atualizarQtde(-1);
}
public void atualizarQtde(int valor) {
DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebase();
String usuarioCodificado = Base64Custom.codificarBase64(usuario.getEmail());
DatabaseReference pCurtidasRef = firebaseRef.child("postagens-curtidas").child(preferencias.getFeedId()).child("qtdCurtidas");
setQtdCurtidas(getQtdCurtidas() + valor);
pCurtidasRef.setValue(getQtdCurtidas());
}
public Feed getFeed() {
return feed;
}
public void setFeed(Feed feed) {
this.feed = feed;
}
public Usuario getUsuario() {
return usuario;
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
public int getQtdCurtidas() {
return qtdCurtidas;
}
public void setQtdCurtidas(int qtdCurtidas) {
this.qtdCurtidas = qtdCurtidas;
}
}
Oops.. I made an error, it n is an Adapter and yes a model. How would you recover that context in a model?
– Luis Felipe
You have a constructor where you pass the values pro model?
– Leonardo Dias
I will edit my reply showing the full model.
– Luis Felipe
I edited! the only problem is the context q still n managed to recover!
– Luis Felipe
I made an issue in my reply, take a look
– Leonardo Dias
Attempt to invoke virtual method 'android.content.Sharedpreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null Object Reference. Gave this error, I’m using to call final Postliked = new Postliked(context);
– Luis Felipe
You’re passing like this or context?
– Leonardo Dias
As where I’m calling the Postliked is an Adpter(Feedadapter ) I’m using the context, q did basically the same thing to recover the context, that you told me to do in this.
– Luis Felipe