Insert SQL values in Android Spinner

0

I select a table and it returns a complete row. I put that row into an object. My intention is to collect the value and inflate a spinner so that if it finds for example in the select: "8" I generate a spinner with values from 1 to 8. I have found several guides online but I am unable to do so. Any help?

XML:

<LinearLayout
    android:id="@+id/manejadorLayout"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:layout_below="@+id/textLayout">

    <TextView
        android:id="@+id/textViewVnt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:text="Vender:" />

    <Spinner
        android:id="@+id/Sp"
        android:layout_width="200dp"
        android:layout_height="match_parent"></Spinner>

    <Button
        android:id="@+id/buttonInvertirTick"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp"
        android:background="@drawable/success" />


</LinearLayout>

Java:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_amazon_venta);

    sp = (Spinner) findViewById(R.id.Sp); 
    textView = (TextView) findViewById(R.id.textViewTextoV);

    opera = new OperacionesBaseDatos();
    carteraObj = opera.leerCartera(10);

    if (carteraObj.getCantidad() > 0) {

        textView.setText("Dispone de " + carteraObj.getCantidad() + " acciones de Amazon INC con un coste de " + carteraObj.getPrecio() + " de media. Actualmente se cotiza a la acción");
    } else {

        textView.setText("No dispone de ninguna accion en propieded de esta entidad");
    }
}

The values to insert in the spinner would be obtained with portfolioObj.getQuantity () and would go inside the if (portfolioObj.getQuantity ()> 0)

    
asked by Eduardo 04.06.2017 в 19:00
source

1 answer

1

What you should do is iterate until you reach that quantity value and build an arrangement (for simplicity I suggest Strings) with the values.

String[] valores = new String[cantidad];
for(int x =0; i< cantidad ; i++){
valores[x]=""+x;
}

and use that array to build the adapter that you are going to set in the spinner

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        valores, android.R.layout.simple_spinner_item);
    
answered by 04.06.2017 / 21:34
source