I asked a question about how to pass data between activities, and they answered me well, but my problem now is that I do not need to pass a simple string or an integer, I need to pass a query SQLserver, put it in the intent and then pass it to the other activity to show me there, but I do not know how to do it, passing plain text is easy, but I do not understand what to do with a SQLserver query, I've been trying for a while and I can not, can someone give me a cable? I leave the code here, thank you!
public class Listado extends AppCompatActivity {
ListView listView;
TextView idtext;
EditText etFiltrar;
String where = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listado);
setupActionBAR();
etFiltrar = (EditText) findViewById(R.id.etFiltrrar);
etFiltrar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
where = etFiltrar.getText().toString();
mostrarClientes();
}
@Override
public void afterTextChanged(Editable s) {
}
});
listView = (ListView) findViewById(R.id.lvMostrar);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ViewGroup vg = (ViewGroup) view;
idtext = (TextView) vg.findViewById(R.id.id);
Log.i("Click", position + " " + idtext.getText());
// ESTO SOLO MUESTRA UN TOAST CON LA POSITION + ID DE TABLA
// Toast.makeText(getApplicationContext(), "Position= " + position + " ID=" + idtext.getText().toString(), Toast.LENGTH_SHORT).show();
**enviardatos()**;
}
});
mostrarClientes();
}
public void **enviardatos**() {
**//Pr**ocedimiento para poder pasarlo a la otra activity, aquí tendría que meter los datos de la consulta sql pero no se como..
Intent intent = new Intent(Listado.this, ClienteEspecifico.class);
intent.putExtra("nombre", "**CADENA SIMPLE, PERO QUIERO PASAR UNA CONSULTA** SQL");
startActivity(intent);
}
private void setupActionBAR() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
//Mostrar el boton en el ActionBar
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("Lista de Clientes");
}
}
public void mostrarClientes() {
HashMap<String, String> clientes = new HashMap<>();
ConectarHaciaSQL consql;
consql = new ConectarHaciaSQL();
Connection cn = consql.conexionBD();
try {
Statement pst = cn.createStatement();
String sel;
sel = "select CLI.idcliente, CLI.NombreComercial, UC.telefono1 from clientes as CLI" + " left join ubicacionesClientes as UC on UC.idcliente = CLI.idCliente ";
if (where != "") {
sel = sel + " where CLI.NombreComercial like '%" + where + "%'";
}
//sel = sel + " ORDER BY CLI.idcliente ASC";
//Ejecuta el query del SQL
ResultSet rs = pst.executeQuery(sel);
//Recorre la tabla del SQL
while (rs.next()) {
clientes.put(rs.getString(1), rs.getString(2));//
}
if (rs != null) {
} else
Toast.makeText(getApplicationContext(),
"No hay nada ", Toast.LENGTH_LONG).show();
if (rs != null)
rs.close();
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_items,
new String[]{"First Line", "Second Line"},
new int[]{R.id.id, R.id.name});
Iterator it = clientes.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultMap.put("First Line", pair.getKey().toString());
resultMap.put("Second Line", pair.getValue().toString());
listItems.add(resultMap);
}
listView.setAdapter(adapter);
} catch (SQLException e) {
Log.i("Error", "Error al abrir o crear la base de datos" + e);
}
}
}
How do you see I have an SQL query that shows names in a listview, but now I need another query, (I guess) as I said, show for example name, phone and some more data in a textview of another activity , or to be able to be each one in an EditText, if someone answers me well I will love him. Thanks !!!
I add the other activity where I want to collect the new data.
public class ClienteEspecifico extends AppCompatActivity {
TextView tvMostrarDatos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cliente_especifico);
tvMostrarDatos = (TextView) findViewById(R.id.tvMostrarDatos);
setupActionBAR();
Bundle bundles = getIntent().getExtras(); //En este objeto es donde están los datos que hemos enviado, pero esto es una cadena simple, necesito la consulta
String nombre = bundles.getString("nombre");
// Integer telefono = bundles.getInt("telefono");
tvMostrarDatos.setText(nombre);
//tvMostrarDatos.setText(telefono);
}
}