How to display a parse string in a Textview?

Asked

Viewed 361 times

2

I’m trying to add an image with a description on the server parse. I’ve already been able to add the image description by objectId, but the problem is that I don’t know how to display the text of the image in a textview.

Any help will be welcome.

package com.parse.starter.Activity;

public class descricaoActivity extends AppCompatActivity {

    private EditText textoDescricao;
    private Button descricaoBotao;
    private TextView descricao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_descricao);

        textoDescricao = (EditText) findViewById(R.id.editTextDescricao);
        descricao = (TextView) findViewById(R.id.descricaoId);
        descricaoBotao = (Button) findViewById(R.id.botaoDescricaoId);

        descricaoBotao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Recupera o objectId da imagem
                Bundle extra = getIntent().getExtras();
                final String objetoId = extra.getString("idObjeto");

                //Recupera o texto digitado pelo usuário
                final String textoDigitado = textoDescricao.getText().toString();

                //Monta o objeto para ser salvo no Parse
                ParseObject parseObject = new ParseObject("Imagem");
                parseObject.setObjectId(objetoId);
                parseObject.put("Nome", textoDigitado);

                //Salva a descrição no objectId da imagem
                parseObject.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) { //Sucesso
                            Toast.makeText(getApplicationContext(),
                                           "Sua descrição foi publicada!",
                                           Toast.LENGTH_LONG).show();

                            Intent intent = new Intent(getApplicationContext(),
                                                       MainActivity.class);
                            startActivity(intent);
                            finish();

                        } else { //Erro
                            Toast.makeText(getApplicationContext(),
                                           "Erro ao postar sua descrição, tente novamente!",
                                           Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        });
    }
}
  • Hey Alekssander. Do you get any error in your code? What happens when it runs? Does not display? Displays wrong?

1 answer

1


To define a text in TextView you must use setText. See below:

TextView descricao = (TextView) findViewById(R.id.descricaoId);
descricao.setText("As vezes é simples!");

You have to observe how the description is set in parse, for example parseObject.getString("descricao");. So you can do it this way:

descricao.setText(parseObject.getString("descricao"));

See other properties of TextView in the documentation.

Browser other questions tagged

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