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

0

I need to assign the data in an object's EditText field, from my form. For example, I'm using an API that looks up the user's address via ViaCep. I am already able to return the data, now I need to play this data in its due 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;
    }
  };

}
    
asked by anonymous 24.08.2017 / 21:29

2 answers

1

In the excerpt that you left commented, after declaring EditText edtCep, simply change to this command:

edtCep.setText(obj.optString("cep"), TextView.BufferType.EDITABLE);
    
24.08.2017 / 21:36
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());
    
24.08.2017 / 21:34