What is the best way to fill a spinner with sqlite?

0

I've had problems with the spinner because the setSeletion () method does not work, I do not know the reasons, but I think it's because of the popular way, that's why I use you to give me ideas, for that I've already used several methods, which would be the correct way to work with spinners, from filling up to consulting a previously saved value,

This is my code to load the spinner, nothing of the ordinary:

public void SPPersona(){
    SQLiteDatabase db = openOrCreateDatabase("SAICoffeeSQL", MODE_PRIVATE, null);
    String qryTP = "select _id, Tipo_persona from TB_Tipo_persona";
    Cursor cTP = db.rawQuery(qryTP,null);
    SimpleCursorAdapter cTPA = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item,  cTP, new String[] {"Tipo_persona"}, new int[] {android.R.id.text1} );
    cTPA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sptipper.setAdapter(cTPA);
    }

This other is to consult:

private void consulta(int id){
    SQLiteDatabase db = openOrCreateDatabase("SAICoffeeSQL", MODE_PRIVATE, null);
    String mqry = "select * from Creditos_productores where _id = " + id;
    cursor = db.rawQuery(mqry,null);

    while(cursor.moveToNext()) {

            int tp = Integer.valueOf(cursor.getString(cursor.getColumnIndex("Id_tipo")));
       sptipper.setSelection(tp);   
    }
}
    
asked by Mark Dev 31.05.2016 в 20:31
source

1 answer

-1

Good gentlemen so many hours to see what it was and it turns out that it is at the time of loading the functions or methods that use bone in the OnCreate () are loaded all the functions that must first be satisfied that they load the spinner with data and then make the query. I mean in the following way:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sulayout);
    cargaSpinner();
    consultaDatos();
}


consultaDatos(){
//Cualquier forma de consultar datos////
}

cargaSpinner{}{
//Cualquier forma de cargar el spinner
}

Good day comes out thanks to all those colleagues who lend themselves and take the time to help you I hope that with this contribution you will be very helpful

    
answered by 01.06.2016 в 15:51