Data sent from one activity to another arrives null

0

My Activity I am displaying the database data in the listview

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

    final ListView listView = (ListView) findViewById(R.id.lvItems);


    GenericDAO g = new GenericDAO(getApplicationContext());
    ArrayList<Cliente> cArray = g.getClientes();

    final List<String> itens = new ArrayList();

    for (int i = 0; i < cArray.size() ; i++) {
        Cliente c = new Cliente();
        c = cArray.get(i);
        itens.add(c.getNome());
    }

    ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itens);
    listView.setAdapter(itemsAdapter);

    //inserindo evento de click no listView e enviando os dados para a segunda activity

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view,
                                int posicao, long id) {

            long cli = adapter.getItemIdAtPosition(posicao);
            Intent it = new Intent(getBaseContext(), Tela_Emprestimo.class);
            it.putExtra("idCliente", cli);

            startActivityForResult(it, 1);
        }
    });

My second activity where I am receiving the data, but I am not getting the client name

public class Tela_Emprestimo extends AppCompatActivity implements Serializable{
    Cliente cliente;
    TextView txtCliente;

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

        Intent i = getIntent();
        cliente = (Cliente)i.getSerializableExtra("idCliente");
        Toast.makeText(this, "cliente :" + cliente.getNome(), Toast.LENGTH_LONG).show();    
    }
}

The Client class

public class Client implements Serializable {     private int id;     private String name;     private String street;     private String neighborhood;     private String number;     private int id;     private int idIdentifier;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getRua() {
    return rua;
}

public void setRua(String rua) {
    this.rua = rua;
}

public String getBairro() {
    return bairro;
}

public void setBairro(String bairro) {
    this.bairro = bairro;
}

public String getNumero() {
    return numero;
}

public void setNumero(String numero) {
    this.numero = numero;
}

public int getIdcidade() {
    return idcidade;
}

public void setIdcidade(int idcidade) {
    this.idcidade = idcidade;
}

public int getIdFuncionario() {
    return idFuncionario;
}

public void setIdFuncionario(int idFuncionario) {
    this.idFuncionario = idFuncionario;
}
    
asked by anonymous 05.03.2018 / 20:32

2 answers

1

What you're adding to Extra is long , I suppose the client id, not the client (object). When attempting to do, in the ScreenImage Activity, cast for Client

cliente = (Cliente)i.getSerializableExtra("idCliente"); 

result is null ;

See how you're doing:

long cli = adapter.getItemIdAtPosition(posicao);
Intent it = new Intent(getBaseContext(), Tela_Emprestimo.class);
it.putExtra("idCliente", cli);

If you want to pass a Client, you must have a method on the Adapter that returns one, anything like Cliente getItemAtPosition(posicao){...} .

The code looks like this:

Cliente cli = adapter.getItemAtPosition(posicao);
Intent it = new Intent(getBaseContext(), Tela_Emprestimo.class);
it.putExtra("idCliente", cli);

For everything to work as stated, you need to use a custom adapter that can handle Client-type objects.

Alternatively, since the list only uses the client name, use a ArrayList<Cliente> , make the override of the toString() method of the Client class, and get the Client of the item clicked on the method onClick() as follows: Cliente cli = (Cliente)parent.getAdapter().getItem(position); . See an example in this response .

The client class should implement the interface Serializable , it would be preferable that it implement Parcelable .

    
05.03.2018 / 22:24
0

As you are using this form to add the data to intent, if using the client name will be easier to retrieve

String cli = adapter.getItemIdAtPosition(posicao);//Pega o nome do cliente da lista, pois vc populou a lista com um List de nomes dos clientes
Intent it = new Intent(getBaseContext(), Tela_Emprestimo.class);
it.putExtra("idCliente", cli);//Adiciona o nome do cliente a Intent

The variable cli is a String of the client name, in the following activity you should look for a String, like this:

Intent i = getIntent();
String clienteName = i.getStringExtra("idCliente"); //Aqui vc tem o nome do cliente

And then just instantiate your search class in the bank and create a method to get the client from the name that you received in the activity

GenericDAO g = new GenericDAO(this);//usar this se estiver em uma activity
Cliente cli = g.getClienteByNome(clienteName);
    
05.03.2018 / 20:45