Set / select option in Spinner

1

I have ListView with several records and when I click on an item it opens in a new activity taking the value of _id of that item.

I get this _id , I search SQLite, and fill in the fields as follows:

public void carregaValores(int id) {

     DBController ctRegistro = new DBController(this);
     ArrayList<CRegistros> registro = new ArrayList<>(ctRegistro.getRegistro(id));
     this.vTipo = registro.get(0).getTipo();
     this.data.setText(registro.get(0).getData());
     this.horario.setText(registro.get(0).getHorario());
     this.historico.setText(registro.get(0).getHistorico());
     this.vClienteString = registro.get(0).getCliente();
}

The question now is how to set the spinners, since I do not have the position value, only the _id registry to be able to compare.

I tried the following ways (as found by OS and SOpt):

1.

String compareValue = this.vClienteString;
        if (compareValue != null) {
            int spinnerPosition = adapter.getPosition(compareValue);
            spnClientes.setSelection(spinnerPosition);
        }

2.

spnClientes.setSelection(clientes.indexOf(this.vClienteString));

Will I have to do a method that will compare by Cursor to generate Spinner , or does it have some simple form like the ones above?

    
asked by anonymous 06.02.2018 / 11:13

2 answers

0

For now, as an alternative I'm using the same method that generates Spinner, but I put a Integer property, so it null when it is only to generate the spinner, or I pass the _id referring to the item that I want it to be selected:

public ArrayList<CPessoas> getClientes(Integer hdl) {

        // Variável auxiliar int (não poderá comparar int com null)
        // Se null, seta 0 para vir valor padrão
        int hdlInt = (hdl == null) ? 0 : hdl;

        sqLiteDatabase = banco.getReadableDatabase();
        ArrayList<CPessoas> clientes = new ArrayList<>();
        // Cria valor padrão vazio ""
        clientes.add(0, new CPessoas(0, ""));
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT handle, nome FROM clientes ORDER BY nome", null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                int handle = cursor.getInt(cursor.getColumnIndex("handle"));
                String nome = cursor.getString(cursor.getColumnIndex("nome"));
                CPessoas cliente = new CPessoas(handle, nome);

                // Compara o valor passado com o valor a ser gerado pelo cursor
                if (hdlInt != handle) {
                    // Caso não for igual, adiciona o item
                    clientes.add(cliente);
                } else {
                    // Caso for igual, adiciona o item com index 0
                    clientes.add(0, cliente);
                }

            } while (cursor.moveToNext());
        }

        return clientes;
    }
    
06.02.2018 / 12:49
0

I have a method where I fill my combos (Spinner) on a screen. This method receives a list (which in this case is an array of enumerators), the spinner I want to populate, the item to be selected (in this case an enum as well) and the context.

You can substitute values for a List of any object of the type you want, but be sure to replace selected with it list object.

I'm sorry I do not adjust the method for objects, as my application is simple and uses enumerators in the spinner list.

public static void montarCombo(Enum[] values, Spinner spinner, EnumValue selected, Context context) {
        List<EnumValue> list = new ArrayList<EnumValue>();
        EnumValue select = new EnumValue(-1, Commons.SELECT);

        list.add(select);

        int pos = -1;

        for (int x =0; x < values.length; x++) {
            if (selected != null && values[x].equals(selected.getValue())) {
                pos = x;
            }
            list.add(new EnumValue(values[x].ordinal(), values[x]));
        }

        ArrayAdapter array = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item,  list);
        array.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(array);
        if (pos > -1) {
            spinner.setSelection(pos + 1);
        }
    }
    
06.02.2018 / 12:53