How to assign data from an object to editText on Android?

Asked

Viewed 380 times

0

I need to assign in Edittext fields, from my form, the data of an object. For example, I’m using an API that searches the user’s address for viaCep. I’m already able to return the data, now I need to throw this data in its proper fields.

public void getCEP(String cep){
  RequestParams rp = new RequestParams();

  AsyncHttpClient client = new AsyncHttpClient();
  client.get("http://viacep.com.br/ws/" + cep + "/json/", rp, new TextHttpResponseHandler() {
    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
      Toast.makeText(getBaseContext(), "Problema na conexao!"+statusCode, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, String responseString) {
      try {
        JSONObject obj = new JSONObject(responseString);

        String retorno = "";

        if (!obj.has("erro")) {
          retorno += "\n" + obj.getString("cep");
          retorno += "\n" + obj.getString("logradouro");
          retorno += "\n" + obj.getString("complemento");
          retorno += "\n" + obj.getString("bairro");
          retorno += "\n" + obj.getString("localidade");
          retorno += "\n" + obj.getString("uf");
          retorno += "\n" + obj.getString("ibge");
          retorno += "\n" + obj.getString("gia");

          Toast.makeText(getBaseContext(),"Dados retornados: "+retorno, Toast.LENGTH_LONG).show();

          EditText edtCep = ((EditText) findViewById(R.id.edtCep));
          //edtCep.setText("cep", retorno.);
        }

      } catch (JSONException e){

      }
    }
  });
}

public void btnCadastrar(View view) {

  final EditText edtNome = ((EditText) findViewById(R.id.edtNome));
  final EditText edtSobrenome = ((EditText) findViewById(R.id.edtSobrenome));
  EditText edtCelular = ((EditText) findViewById(R.id.edtCelular));
  EditText edtCep = ((EditText) findViewById(R.id.edtCep));

  final String nome = edtNome.getText().toString();
  String sobrenome = edtSobrenome.getText().toString();
  String celular = edtCelular.getText().toString();
  String cep = edtCep.getText().toString();

  String url = "http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php";

  StringRequest srUrl = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

    }
  }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
  })
  {
    protected Map<String, String > getParams(){
      Map<String, String> params = new HashMap<String, String>();

      params.put("nome", edtNome.getText().toString());
      params.put("sobrenome", edtSobrenome.getText().toString());

      return params;
    }
  };

}

2 answers

1


In the section you left commented, after declaring Edittext edtCep, just switch to this command:

edtCep.setText(obj.optString("cep"), TextView.BufferType.EDITABLE);
  • Right, but where’s the zip code from inside the return object, on that command of yours? return.zip, something like that

  • Uses "obj.getString("cep")" instead of "return"

  • Okay, I’m gonna try...

  • If you are not sure that "obj" data can contain data, use optString instead of getString, with this Edittext will simply become empty instead of generating an exception.

  • Yeah, what you said before, it didn’t work out so well, because I gave a Lod.d... and returned this on the console: "D/CEP: android.support.v7.widget.Appcompatedittext{1b611dab VFED.. CL ..."

  • I did this: edtCep.setText(obj.optString("cep"), Textview.BufferType.EDITABLE); Log. d("CEP", String.valueOf(edtCep));

  • The cep may not be coming in your json. In your "return" string it appears?

  • Yes, I have a Toast that shows all the data that comes in the return object. Here: Toast.makeText(getBaseContext(),"Returned Data: "+return, Toast.LENGTH_LONG). show();

  • Yes, I noticed this Oast. But it’s strange that the cep appears in it and not in editText, since they both use the same obj.getString("cep").

  • Ta, forget what I told you before kkkkk It is that as I already fill the ZIP code to fetch the data, it is obvious that it will not reappear... I did the same with patio and he appeared.... THANKS.

Show 5 more comments

0

Create a class. Something like:

public class Endereco {
    private String logradouro;

    public void setLogradouro(String logradouro) {
         this.logradouro = logradouro;
    }

    public String getLogradouro() {
        return logradouro;
    }
}

In return populate this object

Endereco endereco = new Endereco();
endereco.setLogradouro(obj.getString("logradouro"));

edtCep.setText(endereco.getLogradouro());
  • Yes, so far I know, but how to take this "something" from the object?

  • setText for Edittext needs a second Textview parameter.Buffertype.

  • Create a class just for that? It is necessary?

  • I don’t think so.

Browser other questions tagged

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