Good day, I hope the question is understood, it turns out that I am filling a spinner from MySQL in which I need to bring a name and a price, I do this correctly, but when I obtain what the spinner contains, it brings me the two data: the name and the price then I need it to only bring me the price this is what I have so far:
For this I use the following library:
implementation 'com.loopj.android:android-async-http:1.4.9'
I declare the variables to use
private AsyncHttpClient cliente;
private Spinner spColonias;
I initialize these variables
spColonias = findViewById(R.id.spinnerColonias);
cliente = new AsyncHttpClient();
I create a method to fill the spinner through the url (using PHP for the query)
private void llenarSpinner(){
String url ="http://"+mi_host+"/llenarColoniasantones.php";
cliente.post(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
if (statusCode == 200){
cargarSpinner(new String(responseBody));
}
}
@Override
public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
And finally I load the spinner with the data from the database:
private void cargarSpinner(String respuesta){
ArrayList<Colonias> lista = new ArrayList<>();
try {
JSONArray jsonArreglo = new JSONArray(respuesta);
for (int i = 0; i < jsonArreglo.length(); i++){
Colonias c = new Colonias();
//En esta parte lleno el spinner con los campos que quiero en este caso serian 2
c.setNombreColonia(jsonArreglo.getJSONObject(i).getString("nombre_colonia"));
c.setPrecioColonia(jsonArreglo.getJSONObject(i).getDouble("precio_colonia"));
lista.add(c);
}
ArrayAdapter<Colonias> a = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, lista);
spCantones.setAdapter(a);
}catch (Exception e){
e.printStackTrace();
}
}
And finally the model:
public class Colonias {
String nombreColonia;
int idColonia;
Double precioColonia;
public Colonias(){
}
public Colonias(String nombreColonia, int idColonia, Double precioColonia) {
this.nombreColonia = nombreColonia;
this.idColonia = idColonia;
this.precioColonia = precioColonia;
}
public String getNombreColonia() {
return nombreColonia;
}
public void setNombreColonia(String nombreColonia) {
this.nombreColonia = nombreColonia;
}
public int getIdColonia() {
return idColonia;
}
public void setIdColonia(int idColonia) {
this.idColonia = idColonia;
}
public Double getPrecioColonia() {
return precioColonia;
}
public void setPrecioColonia(Double precioColonia) {
this.precioColonia = precioColonia;
}
@Override
public String toString() {
return nombreColonia + precioColonia;
}
}
The toString method is what I need to return in the spinner in this case is colonyName and colonyPrice .
As a last step I have created a static variable to obtain the price from any part of the application:
public static String precio;
Then I tell it that the variable is going to be equal to what is selected from the spinner :
precio = spColonias.getSelectedItem().toString();
But when I get the result, it brings me the two data, both colony_name and colony_price , so I need it to only bring me the price and skip the part of obtaining the colony name.
I hope the question has been understood, thank you very much in advance.