Retrieve the context of a Class (Model)

Asked

Viewed 111 times

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;
    }
}

1 answer

0


private Context context;


public PostagemCurtida(Context context) {
     this.context = context;
}

And when you call your Model through some class, you pass the context, for example:

PostagemCurtida postagemCurtida = new PostagemCurtida(this);
  • Oops.. I made an error, it n is an Adapter and yes a model. How would you recover that context in a model?

  • You have a constructor where you pass the values pro model?

  • I will edit my reply showing the full model.

  • I edited! the only problem is the context q still n managed to recover!

  • I made an issue in my reply, take a look

  • 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);

  • You’re passing like this or context?

  • 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.

Show 3 more comments

Browser other questions tagged

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