1
Hello, I’m in trouble when I select a client in a Listview, I have to pass his name to a Edittext of the other Activity and pass the address also.
But when I select the Customer he is grouping the Name with the Address on the same line.
And I have another problem with toString, how to pass all the data in the table without giving me Nullpointer or values appear NULL
.
Follows codes:
{
/**
* Método de listagem dos clientes
*/
public List<Cliente> listarClientes() {
// List que recebe os dados que são percorridos no while
List<Cliente> listaClientes = new ArrayList<Cliente>();
// Variável para utilizar a query no Cursor
String sql = "select * from dj_tb_cli";
// Objeto que recebe os registros do Banco de Dados
Cursor cursor = getReadableDatabase().rawQuery(sql, null);
try {
// Percorre todos os registros do cursor
while (cursor.moveToNext()) {
Cliente cliente = new Cliente();
// Carrega os atributos do Banco de Dados
cliente.setId(cursor.getLong(0));
cliente.setNomeCliente(cursor.getString(1));
cliente.setEnderecoCliente(cursor.getString(6));
listaClientes.add(cliente);
Log.i(TAG, "Listando Clientes");
}
} catch (Exception e) {
Log.i(TAG, e.getMessage());
} finally {
// Garante o fechamento do Banco de Dados
cursor.close();
}
return listaClientes;
}
}
My Listview.
public class ListaClientesBD extends ListActivity implements OnItemClickListener {
private String clienteSel;
private String endSel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabasesDAO db = new DatabasesDAO(this);
setListAdapter(new ArrayAdapter<Cliente>(this,
android.R.layout.simple_list_item_1, db.listarClientes()));
ListView listView = getListView();
listView.setOnItemClickListener(this);
}
// private List<String> listarClientes() {
// return Arrays.asList("Cliente 1", "Cliente 2", "Cliente 3");
// }
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView textView = (TextView) view;
String mensagem = "Cliente Selecionado: " + textView.getText();
Toast.makeText(getApplicationContext(), mensagem, Toast.LENGTH_SHORT)
.show();
// startActivity(new Intent(this, NovoPedidoActivity.class));
Intent intent = new Intent(ListaClientesBD.this,
TelaNovoPedidoActivity.class);
clienteSel = ((TextView) view).getText().toString();
intent.putExtra("cliente", clienteSel.toString());
endSel = ((TextView) view).getText().toString();
intent.putExtra("endCliente", endSel.toString());
startActivity(intent);
}
This snippet is in my Activity. It receives the values by the key of the selected item in the list.
nomeCliente = (EditText) findViewById(R.id.nmClienteBD);
String parametroCli = getIntent().getStringExtra("cliente");
nomeCliente.setText(parametroCli);
enderecoCliente = (EditText) findViewById(R.id.endClienteBD);
String parametroEndCli = getIntent().getStringExtra("endCliente");
enderecoCliente.setText(parametroEndCli);
EditText nomeProduto = (EditText) findViewById(R.id.produtoBD);
String parametroProd = getIntent().getStringExtra("nmProd");
nomeProduto.setText(parametroProd);
I was away for a while, I’ll take a look and tell you if it worked.. I got into some problems here in the development that got stuck..
– user2362558