Populate Spinner from SQL

2

Hello, I need to load a Spinner with the data coming from my Database (SQL). For this I have created a class with an id and a description. The problem is that instead of showing the description, in the Spinner the name of the package is shown to me. I leave the code just in case. Thank you very much.

private void poblarSpinnerObra(ArrayList<ObrasCuenta> list) {
    ArrayAdapter<ObrasCuenta> adapter =
            new ArrayAdapter<ObrasCuenta>(MejorasFragment.this.getContext(), android.R.layout.simple_spinner_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

    spinnerObraMej.setAdapter(adapter);

    spinnerObraMej.setOnItemSelectedListener(
            new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    if ((pos!=0) && (id!=0)) {
                        // buscar comprobantes adeudados
                        Object item = parent.getItemAtPosition(pos);
                        idSubsistema = ((ObrasCuenta) item).getIdObra();
                        buscarDeuda();
                        cargarRecyclerView();
                    }
                }
                public void onNothingSelected(AdapterView<?> parent) {}
            });
}

With the previous code (taken from the Internet) I try to populate the Spinner with an ArrayList but it is visualized in the following way. The behavior in the event is the desired one. I clarify in case it serves as something that the data is in a Fragment

    
asked by DavidC 02.03.2018 в 14:43
source

1 answer

0

The problem is that the representation of an object is being printed since you are using a ArrayAdapter of objects type ObrasCuenta :

 ArrayAdapter<ObrasCuenta>

You have two options, create a custom Adapter or this option that is the simplest,

simply overwrites the toString() method of your ObrasCuenta object by returning the variable containing the description

   @Override
    public String toString() {
        return description;
    }

in this way instead of getting this:

You will get the value of the object you want in the Spinner.

    
answered by 03.03.2018 / 01:48
source