I try to retrieve a record from a bd sqlite and show it on an android studio spinner

1

This is my code but I have errors, someone can kindly help me.

This is my code:

btnBuscarUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            String code= et1.getText().toString();
            fila =db.rawQuery("select User,Pass,Status,Email  from Usuario  where Code='" + code +"'" ,null);
            if (fila.moveToFirst())
            {
                et2.setText(fila.getString(0));
                et3.setText(fila.getString(1));
                spinner.setSelection(fila.getString());
                et5.setText(fila.getString(3));
            }
            else {
                Toast.makeText(getApplicationContext(), "No existe  un usuario  con dicho  código", Toast.LENGTH_SHORT).toString();
                db.close();
            }
        }
    });
    
asked by Sofia 10.01.2017 в 20:46
source

3 answers

1

Friend .... I gave headaches this detail I share my solution, of course this depends on how you fill (popular) the spinner,

first I create a function to fill the spinner:

  private void spGenero() {
    final String TAG_TABLE = "Tb_Sexo";

    List<String> glist = new ArrayList<>();
    SQLiteDatabase db = openOrCreateDatabase("db", android.content.Context.MODE_PRIVATE, null);
    String query = "select * from " + TAG_TABLE;
    Cursor ps = db.rawQuery(query, null);
    while (ps.moveToNext()) {
        glist.add(ps.getString(ps.getColumnIndex("Sexo")));
    }
    db.close();
    ps.close();
    ArrayAdapter saa = new ArrayAdapter(getActivity(), R.layout.row_spinnerii, glist);
    saa.setDropDownViewResource(R.layout.row_spinneiilist);
    spn_genero.setAdapter(saa);

    spn_genero.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            id_gen = getIDProS(parent.getItemAtPosition(position).toString(), TAG_TABLE, "Sexo", "ID");
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}

second you create another function this will help you to position the element you want in the spinner:

private void SeleccionaItem(Spinner spinner, String value) {
    for (int i = 0; i < spinner.getCount(); i++) {
        if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(value)) {
            spinner.setSelection(i);
        }
    }
}

third, this is when you are obtaining or consulting the data of:

...
SeleccionaItem(tu_spinner, fila.getString(2));
....

and finally, check when you load the functions:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_datoslote);

spGenero();


funcion_consulta();


}
    
answered by 13.01.2017 / 02:05
source
2

You are not indicating which value of your query the spinner uses, you only use setSelection() but this is to select an element of your Spinner.

  spinner.setSelection(<indice del elemento>);

To add data to your Spinner this is the correct way:

//Crea ArrayList para almacenar User
List<String> usuarios = new ArrayList<String>();
   SQLiteDatabase db = dbHelper.getWritableDatabase();
        String code= et1.getText().toString();
  fila =db.rawQuery("select User,Pass,Status,Email  from Usuario  where Code='" + code +"'" ,null);
            if (fila.moveToFirst())
            {   
                //Agrega usuarios.
                usuarios.add(fila.getString(0));

                et2.setText(fila.getString(0));
                et3.setText(fila.getString(1));
                et5.setText(fila.getString(3));
            } else {
            Toast.makeText(getApplicationContext(), "No existe  un usuario  con dicho  código", Toast.LENGTH_SHORT).toString();
            db.close();
          }


        // Crea Adapter para Spinner.
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, usuarios);

        // agrega datos al Spinner
        spinner.setAdapter(dataAdapter);

As for the Query, you can also have as an additional parameter an array with the values that will be represented in the query by means of "?":

//fila =db.rawQuery("select User,Pass,Status,Email  from Usuario  where Code='" + code +"';" ,null);

fila =db.rawQuery("select User,Pass,Status,Email  from Usuario  where Code=?" , new String[] {'" + code +"'});
    
answered by 10.01.2017 в 21:23
1

Put the semicolon (;) at the end of the query.

fila =db.rawQuery("select User,Pass,Status,Email  from Usuario  where Code='" + code +"';" ,null);
    
answered by 10.01.2017 в 20:56