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;
}
I modified the client class to installable and added the methods in it, only it is giving error in - Client cli = Adapter.getItemAtPosition(position);
– Aluisio Gomes
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.
– ramaral
More same using serializable, is appearing error in Client cli = Adapter.getItemPosition(position); says cli has to be long type
– Aluisio Gomes
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.– ramaral