Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid : set selected item of a Spinner from DB with Cursor
    primarykey
    data
    text
    <p>I have a problem with Cursors. I have 3 tables in my DB : facture (means invoice), vehicule (vehicle) and garage.</p> <p>An invoice concerns one vehicle and one garage. When I create an invoice, I select the vehicle and the garage from spinners.</p> <p>When I want to update an invoice, I need to set the item selected in these spinners.</p> <p>Here is how I do : </p> <pre><code> for (int iVhc = 0; iVhc &lt; spListeVhc.getCount(); iVhc++) { Cursor valueVhc = (Cursor) spListeVhc.getItemAtPosition(iVhc); long idVhc = facture.getLong(facture.getColumnIndexOrThrow("TB_VEHICULE._id")); long idSpVhc = valueVhc.getLong(valueVhc.getColumnIndex("TB_VEHICULE._id")); if (idSpVhc == idVhc) { spListeVhc.setSelection(iVhc); } } for (int iGar = 0; iGar &lt; spListeGar.getCount(); iGar++) { Cursor valueGar = (Cursor) spListeGar.getItemAtPosition(iGar); long idGar = facture.getLong(facture.getColumnIndexOrThrow("TB_GARAGE._id")); long idSpGar = valueGar.getLong(valueGar.getColumnIndex("TB_GARAGE._id")); if (idSpGar == idGar) { spListeGar.setSelection(iGar); } } </code></pre> <p>It works for the garage, but the problem is that, for a reason that I don't understand, the spinner of vehicles takes the same ID than the garage.</p> <p>That means, if the garage selected has the ID 2 in the DB, the selected vehicle will be the vehicle with ID 2 too.</p> <p>??</p> <p>Here is my query to get the invoice:</p> <pre><code>public Cursor recupFacture(long idFacture){ return db.rawQuery("SELECT TB_FACTURE._id, libelle_fa, date_fa, nom_vhc, kilometrage_fa, nom_garage, remarque_fa, date_paie_fa, montant_fa, TB_VEHICULE._id, TB_GARAGE._id" + " FROM TB_FACTURE, TB_VEHICULE, TB_GARAGE" + " WHERE fk_vhc_fa = TB_VEHICULE._id" + " AND fk_gar_fa = TB_GARAGE._id" + " AND TB_FACTURE._id ="+idFacture, null); } </code></pre> <p>And I realised that I have thi kind of mistakes in my log : </p> <pre><code>08-10 12:54:22.431: ERROR/Cursor(17072): requesting column name with table name -- TB_VEHICULE._id </code></pre> <p>And same for garage...</p> <p>Thanks for your help!</p> <p><strong>EDIT :</strong></p> <p>I found a solution.</p> <p>I replaced the TB_GARAGE._id and TB_VEHICULE._id by the fk at the lines : </p> <pre><code>long idVhc = facture.getLong(facture.getColumnIndexOrThrow("TB_VEHICULE._id")); long idGar = facture.getLong(facture.getColumnIndexOrThrow("TB_GARAGE._id")); </code></pre> <p>However, I can't really explain why it works like this but not with the ID. The prefix of the table causes a strange mistake...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload