Data sent from one Activity to another arrives null

Asked

Viewed 84 times

0

My Activity I’m displaying the database data in 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’m getting the data, only I’m not getting the customer’s 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 Customer class

public class Client Implements Serializable { int private id; private string name; Private String street; Private String neighborhood; private string number; private int idcidade; private int idFunctionary;

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

2 answers

1

What you are to include in Extra is a long, I suppose the Customer ID, not the Customer(object).
When trying to do, in Activity Tela_loan, the cast for Client

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

the 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 Customer, you must have a method in the Adapter that returns one, something like Cliente getItemAtPosition(posicao){...}.

The code would look like this:

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

For everything to work as it was said, you need to use a custom Adapter that can handle Customer-type objects.

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

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

  • I modified the client class to installable and added the methods in it, only it is giving error in - Client cli = Adapter.getItemAtPosition(position);

  • This method has to be written by you. I think that, to avoid new problems, it would be better to first make it work with Serializable before changing to Parcelable.

  • More same using serializable, is appearing error in Client cli = Adapter.getItemPosition(position); says cli has to be long type

  • You must create, if not, a method in the Adapter that returns the Client corresponding to the posicao. The name I used in the answer(getItemAtPosition()) was just one example.

0

As you are using this way to add the data to Intent, using the client name will be easier to recover

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 search 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 research class in the bank and create a method to catch the customer from the name you received at Activity

GenericDAO g = new GenericDAO(this);//usar this se estiver em uma activity
Cliente cli = g.getClienteByNome(clienteName);
  • the problem and why is not passing the client name to second Activity, when I put to display by Toast.makeText() appears that is null

  • You are in a grotesque flaw of logic, and you are using that Arrayadapter that is very generic, and when you call the Adapter again you are calling the Arrayadapter names that you created earlier, now if you do the following, create a variable with the list of names in Activity, after the click of the list you take the position and search in the list, you will have the whole object of the client

Browser other questions tagged

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